Converting python time stamp to day of year
12,296
Solution 1
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
Solution 2
First, you can convert it to a datetime.datetime
object like this:
>>> import datetime
>>> format = "%Y-%m-%d %H:%M:%S"
>>> s = "2015-06-01 00:00:00"
>>> dt = datetime.datetime.strptime(s, format)
Now you can use the methods on dt
to get what you want,except that dt
doesn't have the function you want directly, so you need to convert to a time tuple
>>> tt = dt.timetuple()
>>> tt.tm_yday
152
Related videos on Youtube
Author by
user308827
Updated on June 04, 2022Comments
-
user308827 almost 2 years
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)
-
user308827 over 7 years@Whud, I want a number where first day of year is 1...
-
-
Aman Jaiswal over 7 yearsI just copy the date from the question that's it. it is not a Plagiarism..and while answering I didn't see the above answer
-
MattDMo over 7 yearsI'm terribly sorry! I didn't realize at all the date was in the question. My apologies!