3

If you use the following code:

set CurrentTime to (time string of (current date))

The CurrentTime will be set in the following format:

12:02:38 PM

However, I want CurrentTime to be:

12:02 PM

So, I want to remove the characters in the 6, 7, and 8 positions in the string.

How can this be accomplished in AppleScript? Thank you.

3

You can use something like this:

set CurrentTime to (do shell script "date +\"%l:%M %p\" | awk '{$1=$1;print}'")

There's more info about date and its modifiers at cyberciti.

%l - hour (1..12)
%M - minute (00..59)
%p - locale’s equivalent of either AM or PM; blank if not known
  • Thanks for the solution. Your command changes the format of the hour figure slightly. Instead of "3:16:47 PM" I get "03:16 PM". Is it possible to have single-digit-hours without a zero in front (i.e., in the same format as time string of (current date))? – rubik's sphere Nov 8 '16 at 20:17
  • @rubik'ssphere updated the answer. Use %l (lowercase L) for hour format. – Mateusz Szlosek Nov 8 '16 at 20:18
  • The zero is gone, except now an additional space is added in front of the string. E.g., " 3:16 PM". Is it possible to have no space? – rubik's sphere Nov 8 '16 at 21:09
  • @rubik'ssphere added awk to trim leading space. – Mateusz Szlosek Nov 8 '16 at 21:43
3

Here is an AppleScript subroutine I found doing a Google search that does what you asked.

on getTimeInHoursAndMinutes()
    -- Get the "hour"
    set timeStr to time string of (current date)
    set Pos to offset of ":" in timeStr
    set theHour to characters 1 thru (Pos - 1) of timeStr as string
    set timeStr to characters (Pos + 1) through end of timeStr as string

    -- Get the "minute"
    set Pos to offset of ":" in timeStr
    set theMin to characters 1 thru (Pos - 1) of timeStr as string
    set timeStr to characters (Pos + 1) through end of timeStr as string

    --Get "AM or PM"
    set Pos to offset of " " in timeStr
    set theSfx to characters (Pos + 1) through end of timeStr as string

    return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes

set CurrentTime to getTimeInHoursAndMinutes()
1

One AppleScript-only option is:

# your line
set CurrentTime to (time string of (current date))
# place all the words of the time string into a list
set EveryWord to every word of CurrentTime
# do a very basic test on what we got and assemble the string using the parts plus separators
if length of EveryWord = 4 then
    # if we have 4 words, there must be a form of AM/PM
    set CurrentTime to item 1 of EveryWord & ":" & item 2 of EveryWord & " " & item 4 of EveryWord
else
    # 24-hour clock
    set CurrentTime to item 1 of EveryWord & ":" & item 2 of EveryWord
end if
  • That's an interesting way however the else branch will probably never trigger as I believe the time element of current date is not presented in 24 hour time and there will always be an AM or PM. Although I guess one might be able to change the defaults from where current date get its information. – user3439894 Nov 14 '16 at 0:29
  • current date will return the date and time in the currently set localization. I got to know that painfully when I relied on it to respond in the US-way and my script stopped working on a colleague's computer. During tests, I found that when setting the system clock to a 24-hour time it changed the output. On the contrary hours of (current date) will always return the hours passed since midnight (= 24-hour way). So, I recommend using the date & time parts when verifying the current date & time. You additionally have the benefit of outputting it the way you like with that. – Phoenix Nov 14 '16 at 4:24
  • I'm using US English with default settings and current date outputs e.g. "Monday, November 14, 2016 9:44:45 AM" whether or not I have the [] Use 24-hour clock checkbox checked or not and is why I made the first comment. On my system the else branch never triggers. – user3439894 Nov 14 '16 at 14:45
  • First of all, if you use the same system setup on each computer you work on, you will possibly not come across it. Also, changing the time display of the clock menu extra is not changing anything else other than that. You would need to go to "Language & Region" to enable the option "Time Format [x] 24-Hour Time". My example snippet will run on every computer with the OP's desired output and not only those having the display of "AM" and "PM" (or their regional counterparts). If this will never happen to you, feel free to just use the condition working for you, stripping the rest. – Phoenix Nov 14 '16 at 15:24
  • You said, "You would need to go to "Language & Region" to enable the option "Time Format [x] 24-Hour Time". ", I do not have that check box at that location. Anyway as I already said your answer provides an interesting method and another method for rubies' sphere to use. – user3439894 Nov 14 '16 at 15:30
0

This is simplistic, but I used it to solve a similar problem and it works for your example. It just takes off the AM or PM, deletes the characters you don't want, then adds the AM or PM on again. The last line uses display dialog to show the result.

set CurrentTime to (time string of (current date))
set revisedTime to CurrentTime as string
set myAMPM to text -1 thru -3 of revisedTime
set revisedTime to text 1 thru -7 of revisedTime
set revisedTime to revisedTime & myAMPM
display dialog revisedTime

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .