How to convert 12hour format to 24 hour format?

Hi,

I have a variable fourthPart that typically ends up with a string value of 5pm or 11:30am (among many others)

I need to convert it to 24 format like:

5pm → 17:00 → final format would need to look like 17_00_00

11:30am → 11:30 → final format needs to look like 11_30_00

I have found alot of date conversion info on macscripter, but can’t seem to find anything regarding this.

Any pointers greatly appreciated.

Jeff

The crunch is in that “(among many others)”, of course, but this should get you started:


-- This assumes that the input is a 12-hour time string, that any separators in it are colons, and that it ends with either "am" or "pm".
on h12toh24(input, separator)
	set t to words of text 1 thru -3 of input
	if (text -2 thru -1 of input is "pm") then set item 1 of t to (item 1 of t) + 12
	repeat 3 - (count t) times
		set end of t to 0
	end repeat
	tell (1000000 + (item 1 of t) * 10000 + (item 2 of t) * 100 + (item 3 of t)) as text
		return text 2 thru 3 & separator & text 4 thru 5 & separator & text 6 thru 7
	end tell
end h12toh24

set fourthPart to "5pm"

h12toh24(fourthPart, "_")

Hi Nigel,

It worked fantastically. Thank You. If you have paypal donation or something let me know.

I appreciate it really, it makes my life a little easier.

Thanks so much,

Jeff

I tried the opposite. I tried to convert a time from the 24 hour format to the 12 hour format because my computer is set the the US date format.


set theDate to "March 1, 2021 at 9:00"

set theTime to word 5 of theDate & ":" & word 6 of theDate

if word 1 of the theTime > 12 then
	set theTime2 to (word 1 of theTime) - 12 & ":" & word 2 of theTime & "PM" as text
else
	set theTime2 to word 1 of theTime & ":" & word 2 of theTime & "AM" as text
end if

However, there’s a bug. if the time is supposed to be 9:00AM, it becomes -3:00PM.
It works if the initial time to 10:00. Weird.

Hi.

Word 1 of theTime is a word — ie. text. If it’s, say, “12”, it counts as less than “9” because it begins with “1”. You need to convert the word to a number before comparing:


set theDate to "March 1, 2021 at 15:00"

set theTime to text from word 5 to word 6 of theDate

if ((word 1 of the theTime) as integer > 12) then
	set theTime2 to (((word 1 of theTime) - 12) as text) & ":" & word 2 of theTime & "PM"
else
	set theTime2 to theTime & "AM"
end if

Edit: Code tidied up a little.

My reply above, while correct, isn’t really complete.

AppleScript automatically coerces between numeric text and the number classes (and between the number classes) as required. But how it does it depends on how obvious it is what the coder means.

If ‘word 1 of theTime’ is numeric text, then (word 1 of theTime) - 12 subtracts 12 from the number represented by the word, because the minus sign makes it clear that an arithmetical operation is to be done. It would be the same if the 12 was text too.

But with comparisons and concatenations, the rule is that the class of the value to the left of the operator determines what kind of comparison or contenation is done. So if ‘word 1 of theTime’ is the text “9”, (word 1 of theTime) > 12 is interpreted as “9” > “12”, which is true because the first character of “9” is lexically greater than the first character of “12”. But if the comparison were written the other way round as 12 < (word 1 of theTime), then it would be done numerically as 12 < 9 because the value to the left of the operator’s an integer.

Another possibility when comparing numeric strings is to use AppleScript’s considering numeric strings attribute, which forces numbers in strings to be compared numerically rather than lexically. It seems to work with mixtures of strings and numbers too, but I don’t know if that’s intentional.


set theDate to "March 1, 2021 at 15:00"

set theTime to text from word 5 to word 6 of theDate

considering numeric strings
	if ((word 1 of the theTime) > "12") then
		set theTime2 to (((word 1 of theTime) - 12) as text) & ":" & word 2 of theTime & "PM"
	else
		set theTime2 to theTime & "AM"
	end if
end considering

Thank you, I’ll try this out. I’ve never encountered the “considering” statements before.