Converting time to fraction of days in python

This is the place for queries that don't fit in any of the other categories.

Converting time to fraction of days in python

Postby Anusha » Wed Nov 06, 2013 2:46 pm

Hi all,

How do I convert date and time into fraction of days format ?

To be clearer, I have a list of timedelta objects like 26 days,0:16:53. Is there any way to convert this into one number that would be the equivalent value of the above in the format of days only ( fraction value). Example 26.XXXX where 'XXXX' is the fraction of a day corresponding to "0:16:53" in the above example. Is there any way to do this ?

Any help is appreciated.

Thanks in advance
Anusha
 
Posts: 2
Joined: Wed Nov 06, 2013 2:42 pm

Re: Converting time to fraction of days in python

Postby stranac » Wed Nov 06, 2013 3:55 pm

It should be pretty simple. Just divide the number of seconds in your timedelta by the number of seconds in a day.
Depending on what you want(using your example):
Code: Select all
>>> td.seconds / 86400.
0.011724537037037037
>>> td.total_seconds() / 86400.
26.011724537037036
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Converting time to fraction of days in python

Postby Anusha » Wed Nov 06, 2013 4:13 pm

thank you so much..! it works !
Anusha
 
Posts: 2
Joined: Wed Nov 06, 2013 2:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 3 guests