[Tutor] 12 hr to 24 hr time conversion
Joel Goldstick
joel.goldstick at gmail.com
Wed Oct 26 11:33:14 CEST 2011
On Wed, Oct 26, 2011 at 4:46 AM, Praveen Singh <c2praveen30jun at gmail.com>wrote:
> How can i convert this-
>
> >>>time24hr('12:34am')
> '0034hr'
>
> i searched in date module but i am not able to figure out what how to do
> this...
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
You can do this with slices.
if t = '13:34am' then t[-2:] gives you 'am'
I'm not sure if your time will sometimes be like this: '03:34' or like this
'3:34'. Assuming that the hours could be one or two digits makes slicing a
problem, but you can use split() like this:
pieces = t.split(":")
Here is what I played with:
>>> s = '12:34am'
>>> s[:-2]
'12:34'
>>> s[-2]
'a'
>>> s[-2:]
'am'
>>> s[:-2].split(':')
['12', '34']
>>>
So, you can get the pieces with those tools. Test the value for first
value, and am/pm. If am and 12 then change 12 to 0. If pm and > 12, then
add 12 to the number.
Then put the pieces back together
--
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111026/0bf499c7/attachment.html>
More information about the Tutor
mailing list