Hey,
Found a script to convert standard time to military but does anyone have one to convert military time
to standard?
Thanks,
Carl
Model: Mac Mini
Browser: Safari 531.22.7
Operating System: Mac OS X (10.5)
Hey,
Found a script to convert standard time to military but does anyone have one to convert military time
to standard?
Thanks,
Carl
Model: Mac Mini
Browser: Safari 531.22.7
Operating System: Mac OS X (10.5)
It doesn’t seem hard. Just check the hour. If it’s greater than 12 then subtract 12 from the hour and append PM, if it’s 12 then append PM, otherwise just append AM. Very simple.
Thanks a bunch…complete noob at this, might you have a short sample to show how I’d construct it?
Trying to arrive at this format 11:30 pm etc.
Carl
Try this…
fixed this script... see my post in #6
Hi, Hank. Your script doesn’t work between midnight and 0100 hours.
set militaryTime to "0030 hours"
set standardTime to militaryTimeToStandardTime(militaryTime)
on militaryTimeToStandardTime(militaryTime)
set militaryTime to militaryTime as text
set theHour to text 1 thru 2 of militaryTime
set theMinutes to text 3 thru 4 of militaryTime
set theHour12 to text 2 thru 3 of ((101 + (theHour + 11) mod 12) as text)
set AMorPM to item (((theHour > 11) as integer) + 1) of {" AM", " PM"}
return theHour12 & ":" & theMinutes & AMorPM
end militaryTimeToStandardTime
Good catch Nigel. I see your script works well. I really don’t know how you come up with those mod statements! Just for completeness I’ll fix mine by adding one more “else if” statement. It’s a little easier for my brain to work with them.
for my really really correct script see my post in #8
Thanks guys. Really appreciate the help!
Carl
I found what I think could be another mistake. Is midnight 2400 or 0000? Just in case, this script accounts for both…
OK, OK. Just see my post in #10!!!
Er . It still needs leading zeros for PM times (or their removal elsewhere).
Yikes Nigel. I think removal of them is better. One more time!
set militaryTime to "0100 hours"
set standardTime to militaryTimeToStandardTime(militaryTime)
on militaryTimeToStandardTime(militaryTime)
set militaryTime to militaryTime as text
set theHour to text 1 thru 2 of militaryTime
set theMinutes to text 3 thru 4 of militaryTime
set theHourNumber to theHour as number
if theHourNumber is equal to 24 or theHourNumber is equal to 0 then
set standardTime to "12" & ":" & theMinutes & " AM"
else if theHourNumber is greater than 12 then
set theHourNumber to theHourNumber - 12
set standardTime to (theHourNumber as text) & ":" & theMinutes & " PM"
else if theHourNumber is equal to 12 then
set standardTime to theHour & ":" & theMinutes & " PM"
else
set standardTime to (theHourNumber as text) & ":" & theMinutes & " AM"
end if
return standardTime
end militaryTimeToStandardTime
Heres my beefed up code :
set theStart to "HHMM"
set thExit to false
repeat
tell application "Finder"
display dialog "Enter military time and click continue to convert to standard time." default answer theStart buttons {"Exit", "Continue"} default button 2 with title "Military --> Standard"
end tell
set theResult to result
set theStart to "HHMM"
if button returned of theResult = "Exit" then
exit repeat
end if
if (length of text returned of theResult) = 4 then
set theHrs to (text 1 thru 2 of text returned of theResult) as number
set theMins to (text 3 thru 4 of text returned of theResult) as number
try
theHrs as number
theMins as number
if theHrs as number > 23 or theHrs as number < 0 or theMins as number > 59 or theMins as number < 0 then
GoingToONERRORbecauseThisIsntAVariable
else
if theHrs = 12 then
set AMPM to "PM"
else
if theHrs > 12 then
set theHrs to theHrs - 12
set AMPM to "PM"
else
set AMPM to "AM"
end if
if theHrs = 0 then
set theHrs to 12
end if
end if
if (length of (theMins as string)) = 1 then
set extraSpace to "0"
else
set extraSpace to ""
end if
set newtime to (theHrs as string) & ":" & extraSpace & (theMins as string) & " " & AMPM
tell application "Finder"
display dialog "Converted time:
" & newtime buttons {"Exit", "Again"} default button 2 with title "Military --> Standard"
end tell
if button returned of result is "Exit" then
exit repeat
end if
end if
on error
errormsg(theResult)
if thExit then
exit repeat
end if
end try
else
errormsg(theResult)
if thExit then
exit repeat
end if
end if
end repeat
on errormsg(theResult)
tell application "Finder"
display dialog "The entered military time is not valid. Please check it and continue." buttons {"Exit", "Go back"} default button 2 with title "Military --> Standard - Error"
end tell
if button returned of result is "Exit" then
global thExit
set thExit to true
else
global theStart
set theStart to text returned of theResult
end if
end errormsg
Hi, Alex.
Here’s a compressed form of that:
set theStart to "HHMM"
set thExit to false
repeat until (thExit)
tell application (path to frontmost application as text)
display dialog "Enter military time and click continue to convert to standard time." default answer theStart buttons {"Exit", "Continue"} default button 2 with title "Military --> Standard"
end tell
set {button returned:buttonReturned, text returned:textReturned} to result
set thExit to (buttonReturned is "Exit")
if (not thExit) then
try
if ((length of textReturned) is not 4) then error
set theHrs to (text 1 thru 2 of textReturned) as number
set theMins to (text 3 thru 4 of textReturned)
if (theHrs > 23) or (theHrs < 0) or (theMins > "59") or (theMins < "00") then error
if (theHrs > 11) then
set AMPM to "PM"
if (theHrs > 12) then set theHrs to theHrs - 12
else
set AMPM to "AM"
if (theHrs = 0) then set theHrs to 12
end if
set newtime to (theHrs as string) & ":" & theMins & " " & AMPM
set thExit to (button returned of showMessage("Converted time:
" & newtime, {"Exit", "Again"}, 2, "Military --> Standard") is "Exit")
set theStart to "HHMM"
on error
set thExit to (button returned of showMessage("The entered military time is not valid. Please check it and continue.", {"Exit", "Go back"}, 2, "Military --> Standard - Error") is "Exit")
set theStart to textReturned
end try
end if
end repeat
on showMessage(theMsg, theButtons, defaultButton, theTitle)
tell application (path to frontmost application as text)
display dialog theMsg buttons theButtons default button defaultButton with title theTitle
end tell
return result
end showMessage
Edit: Minor bug fix. :rolleyes:
Be aware that the 12 hour notation (am, pm) is not standard. It Is in the US and Canada a common notation but globally the 24-hour notation is the standard notation which is hh:mm and hh:mm:ss
Thanks Nigel! I like your use of a handler for the repetitive messages, never thought of that And DJ, that would just take the fun out of making a converter for this Haha
I know but just in case you wasn’t aware of it.
Yeah, thanks, I actually tend to forget that a lot, I could never live out east, lol, you live in US or Canada?