user308827 user308827 - 1 month ago 5
Python Question

Converting python time stamp to day of year

How can I convert a python timestamp into day of year:

Timestamp('2015-06-01 00:00:00')


I want a number where Jan 1 is 1, Jan 2 is 2... Dec 31 is 365 (for a non-leap year)

Answer

You might want to take a look at the function datetime.timetuple() which returns a time.struct_time object with your desired attribute. It has a named tuple interface, so you can access the values by index or attribute name.

import datetime

date = datetime.datetime.strptime("2015-06-01 00:00:00",
                                  "%Y-%m-%d %H:%M:%S")
print date.timetuple().tm_yday
#=> 152
Comments