
Convert time from 12-hours and 24-hours formats
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Convert time from 12-hours and 24-hours formats
I'm working in a project where I need both 12-hours and 24-hours time. Is there a quick way to convert the time from and between this two time formats or I have to manually convert them? 

Re: Convert time from 12-hours and 24-hours formats
Hi Mag,
no need to do this manually, do it by script!
...
convert "10:00 PM" from time to system time
answer it
## 22:00
...
Best
Klaus
no need to do this manually, do it by script!

...
convert "10:00 PM" from time to system time
answer it
## 22:00
...

Best
Klaus
Re: Convert time from 12-hours and 24-hours formats
Hi Mag,
also have a look at "twelveHourTime" in the dictionary, also mentioned in the dictionary entry "time"
this will toggle the displayed time from 12 to 24 hours and back each time you click the button
Kind regards
Bernd
also have a look at "twelveHourTime" in the dictionary, also mentioned in the dictionary entry "time"
Code: Select all
on mouseUp
set the twelveHourTime to not the twelveHourTime
put the time into field 1
end mouseUp
Kind regards
Bernd
Re: Convert time from 12-hours and 24-hours formats
Klaus wrote:convert "10:00 PM" from time to system time
answer it
## 22:00
Thank you Klaus, and if the system time is already in 12-hours format? (I don't know this value)
Re: Convert time from 12-hours and 24-hours formats
Can you check if your time contains AM or PM and take appropriate action in that case? 

Re: Convert time from 12-hours and 24-hours formats
Thank you, very interesting. Anyway I have to use both, 12 and 24 time in the same stack. The user enter the time, and with a checkbox it has to see the value in 12 or 24 at any time. This is because I'm searching a way to convert it.
Code: Select all
function convertFrom12to24 myTime
return my24Time
end convertFrom12to24
function convertFrom24to12 myTime
return my12Time
end convertFrom24to12
Last edited by Mag on Sun May 11, 2014 1:00 pm, edited 3 times in total.
Re: Convert time from 12-hours and 24-hours formats
I Klaus, I do know if the time I need to convert is in 12 or 24 format.Klaus wrote:Can you check if your time contains AM or PM and take appropriate action in that case?
Re: Convert time from 12-hours and 24-hours formats
Quick and dirty:
Might need some more details, but is a start.
I'll leave the other function up to you!
Code: Select all
function convertFrom12to24 myTime
put (myTime contains "PM") into isPM
replace " AM" with empty in myTIme
replace " PM" with empty in myTime
set itemdel to ":"
if isPM then
add 12 to item 1 of myTime
end if
return myTime
end convertFrom12to24
I'll leave the other function up to you!

Re: Convert time from 12-hours and 24-hours formats
OK, here is the resulting functions.
Thank you to all helped me!
Some aspects to keep in mind when make this kind of time conversion:
1. In the 24-hours time, there is not the hour 24, maximum time is 23:59.
2. In the 24-hours time, there is the 00 hour which corresponds to 12 AM.
3. The 12 PM, in 12-hours times, corresponds to 12:00 in 24-hours time.
The functions are not localizes, so they expects to find "AM" and "PM" strings. Also they convert only the hour and AM/PM notation, not the whole time. Anyway, it's easy to implement minutes and seconds if needed (they don't change in conversion).
Thank you to all helped me!
Some aspects to keep in mind when make this kind of time conversion:
1. In the 24-hours time, there is not the hour 24, maximum time is 23:59.
2. In the 24-hours time, there is the 00 hour which corresponds to 12 AM.
3. The 12 PM, in 12-hours times, corresponds to 12:00 in 24-hours time.
The functions are not localizes, so they expects to find "AM" and "PM" strings. Also they convert only the hour and AM/PM notation, not the whole time. Anyway, it's easy to implement minutes and seconds if needed (they don't change in conversion).
Code: Select all
function from24to12 tHour -- tHour is the passed hour, for example 14
if tHour = "0" then
put "12" into tHour
put "AM" into tAMorPM
else if tHour = "12" then
put "PM" into tAMorPM
else if tHour > "12" then
subtract 12 from tHour
put "PM" into tAMorPM
else if tHour < "12" then
put "AM" into tAMorPM -- the hour remains the same
end if
end if
return tHour && tAMorPM -- e.g. "2 PM"
end from24to12
Code: Select all
function from12to24 tHour tAMorPM -- e.g 2 PM
if tAMorPM = "AM" then
if tHour = "12" then
put "00" into tHour
end if
else if tAMorPM = "PM" then
if tHour <> "12" then
add "12" to tHour
else if tHour = "12" then
put "12" into tHour
end if
end if
return tHour -- e.g 14
end from12to24