Community discussions

 
tabate47
Member
Member
Topic Author
Posts: 435
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Convert Time from 24 Hour to 12 Hour

Sat Nov 24, 2018 6:37 pm

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!
 
tabate47
Member
Member
Topic Author
Posts: 435
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Convert Time from 24 Hour to 12 Hour

Sun Nov 25, 2018 8:30 am

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?
 
tabate47
Member
Member
Topic Author
Posts: 435
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Convert Time from 24 Hour to 12 Hour

Mon Nov 26, 2018 2:16 am

What did I do? No one likes me!
 
millenium7
Member Candidate
Member Candidate
Posts: 192
Joined: Wed Mar 16, 2016 6:12 am

Re: Convert Time from 24 Hour to 12 Hour

Mon Nov 26, 2018 3:54 am

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
 
tabate47
Member
Member
Topic Author
Posts: 435
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Convert Time from 24 Hour to 12 Hour

Mon Nov 26, 2018 5:23 am

Thanks. I’ll try it.

What if it’s midnight?

00:00:00?
 
crazymags
just joined
Posts: 4
Joined: Tue Jan 30, 2018 7:27 am

Re: Convert Time from 24 Hour to 12 Hour

Mon Nov 26, 2018 5:52 am

Have you managed to figure it out? Do you still need some help?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1241
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Convert Time from 24 Hour to 12 Hour

Mon Nov 26, 2018 8:05 am

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.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
tabate47
Member
Member
Topic Author
Posts: 435
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Convert Time from 24 Hour to 12 Hour

Mon Nov 26, 2018 5:15 pm

I haven't got it yet. I could use a lot of help!
 
millenium7
Member Candidate
Member Candidate
Posts: 192
Joined: Wed Mar 16, 2016 6:12 am

Re: Convert Time from 24 Hour to 12 Hour

Tue Nov 27, 2018 1:56 am

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
 
tabate47
Member
Member
Topic Author
Posts: 435
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

Re: Convert Time from 24 Hour to 12 Hour

Tue Nov 27, 2018 2:38 am

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.
 
millenium7
Member Candidate
Member Candidate
Posts: 192
Joined: Wed Mar 16, 2016 6:12 am

Re: Convert Time from 24 Hour to 12 Hour

Tue Nov 27, 2018 2:44 am

In that case modify the script, check the first 2 digits and if they equal "00" then change to "12"

Who is online

Users browsing this forum: No registered users and 48 guests