AppleScript – Get Time Without Seconds or Remove Text in Middle of String

applescriptmacostime

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.

Best Answer

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