Page 1 of 1

Convert Time from 24 Hour to 12 Hour

Posted: Sat Nov 24, 2018 6:37 pm
by tabate47
I'm working on a script that needs the time in 12 hour format. Is there a way to convert the time from 24 hour to 12 hour?

Thanks!

Re: Convert Time from 24 Hour to 12 Hour

Posted: Sun Nov 25, 2018 8:30 am
by tabate47
No one?

Is this possible?

When we do system clock get time it outputs something like 17:45. We want it to say 5:45 pm.

Can it be done?

Re: Convert Time from 24 Hour to 12 Hour

Posted: Mon Nov 26, 2018 2:16 am
by tabate47
What did I do? No one likes me!

Re: Convert Time from 24 Hour to 12 Hour

Posted: Mon Nov 26, 2018 3:54 am
by millenium7
Well for one you're in the wrong section and this should probably be in Scripting

This probably isn't the best method but maybe something like

:if (($CurrentTime - 12:00:00) > 00:00:00) do={:set $CurrentTime ($CurrentTime - 12:00:00)}

Havn't tested that works, but theoretically if time is midday or earlier then it won't change the variable, otherwise if its later than midday it'll subtract 12 hours, effectively displaying in 12hr time
You could adjust that to also include AM/PM on the end as a string

Re: Convert Time from 24 Hour to 12 Hour

Posted: Mon Nov 26, 2018 5:23 am
by tabate47
Thanks. I’ll try it.

What if it’s midnight?

00:00:00?

Re: Convert Time from 24 Hour to 12 Hour

Posted: Mon Nov 26, 2018 5:52 am
by crazymags
Have you managed to figure it out? Do you still need some help?

Re: Convert Time from 24 Hour to 12 Hour

Posted: Mon Nov 26, 2018 8:05 am
by Jotne
Why do you need 12 hour. format? You then also need PM/AM to differentiate time of day. This only makes it mor complicated for use in other part since its not only number format.

Re: Convert Time from 24 Hour to 12 Hour

Posted: Mon Nov 26, 2018 5:15 pm
by tabate47
I haven't got it yet. I could use a lot of help!

Re: Convert Time from 24 Hour to 12 Hour

Posted: Tue Nov 27, 2018 1:56 am
by millenium7
I just tested the script I posted above and it works exactly as expected
:global CurrentTime [/system clock get time]
:put "Current time is $CurrentTime"
:if (($CurrentTime - 12:00:00) > 00:00:00) do={:set $CurrentTime ($CurrentTime - 12:00:00) ; :put "Time changed to $CurrentTime"}
Then alter the CurrentTime variable and re-test by pasting the last 2 commands

Alter with either of these commands for testing
:set $CurrentTime 12:00:01
:set $CurrentTime ($CurrentTime+05:00:00)
If time is exactly midday it doesn't change the variable because its a 'greater than' comparison, not 'greater than or equal to'. So midday doesn't quality, only 1 second past which will display as 00:00:01

Re: Convert Time from 24 Hour to 12 Hour

Posted: Tue Nov 27, 2018 2:38 am
by tabate47
Thanks. I was asking about midnight, 00:00:00. Won't that output as 00:00:00 or whatever time it is within that hour? I need it to output 12:15am, or something like that, not 00:15am.

Re: Convert Time from 24 Hour to 12 Hour

Posted: Tue Nov 27, 2018 2:44 am
by millenium7
In that case modify the script, check the first 2 digits and if they equal "00" then change to "12"