Python: Convert 24 hr to 12 hr Time?

Posted on
Sun Jun 13, 2021 12:35 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Python: Convert 24 hr to 12 hr Time?

I have a variable that contains my sprinkler start time in 24 hr format without a colon. e.g. 2230
Looking for a method, other than applescript, to convert it to 12 hr. e.g. 10:30 pm

Appreciate any input on the best way to do it.

Thanks,

Carl

Posted on
Sun Jun 13, 2021 2:08 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Python: Convert 24 hr to 12 hr Time?

a simple script should do it
Code: Select all
old = indigo.variables["varnname'].value
hours = int(old[:-2])
minutes = old[-2:]
ampm = " am" if hours < 12 else " pm"
new = str(hours%12) + minutes + ampm
indigo.updatevariable("varname",new)
 


Karl

Posted on
Sun Jun 13, 2021 4:39 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python: Convert 24 hr to 12 hr Time?

Perfect, thanks for the help!

Edit: Just noticed the colon is missing in the time. 1030 pm. Any way to add that?

Carl

Posted on
Sun Jun 13, 2021 6:15 pm
FlyingDiver offline
User avatar
Posts: 7190
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python: Convert 24 hr to 12 hr Time?

Code: Select all
new = str(hours%12) + " : " + minutes + ampm

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jun 13, 2021 8:20 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Python: Convert 24 hr to 12 hr Time?

Excellent, thanks!

Carl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 8 guests