Convert time from 12-hours and 24-hours formats

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Convert time from 12-hours and 24-hours formats

Post by Mag » Sun May 11, 2014 8:35 am

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? :mrgreen:

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert time from 12-hours and 24-hours formats

Post by Klaus » Sun May 11, 2014 11:56 am

Hi Mag,

no need to do this manually, do it by script! 8)
...
convert "10:00 PM" from time to system time
answer it
## 22:00
...
:D


Best

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Convert time from 12-hours and 24-hours formats

Post by bn » Sun May 11, 2014 12:17 pm

Hi Mag,

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
this will toggle the displayed time from 12 to 24 hours and back each time you click the button

Kind regards
Bernd

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Convert time from 12-hours and 24-hours formats

Post by Mag » Sun May 11, 2014 12:17 pm

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)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert time from 12-hours and 24-hours formats

Post by Klaus » Sun May 11, 2014 12:19 pm

Can you check if your time contains AM or PM and take appropriate action in that case? 8)

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Convert time from 12-hours and 24-hours formats

Post by Mag » Sun May 11, 2014 12:20 pm

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.

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Convert time from 12-hours and 24-hours formats

Post by Mag » Sun May 11, 2014 12:21 pm

Klaus wrote:Can you check if your time contains AM or PM and take appropriate action in that case? 8)
I Klaus, I do know if the time I need to convert is in 12 or 24 format.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Convert time from 12-hours and 24-hours formats

Post by Klaus » Sun May 11, 2014 12:37 pm

Quick and dirty:

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
Might need some more details, but is a start.
I'll leave the other function up to you! 8)

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Convert time from 12-hours and 24-hours formats

Post by Mag » Mon May 12, 2014 4:14 pm

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).

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

Post Reply