Basically what I need is to format a datetime
to get the day of the year and display it in a . That is, if I have, for example datetime
, any one:
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2015, 12, 16, 11, 8, 2, 620935)
I want to convert it to the following format AÑO+DIA_DEL_AÑO
:
10229
The above example means that the product was produced in the year 2010 on the 229th day of the year.
Is there any function, method or library to help me get it?
Having an object
datetime
, you can get the day of the year as follows:Whose output —for the day this answer was written— is:
15350
Now, if you want to generate this text for any date, it will depend on the input format of the date. For example:
If you want something more complete, you can also use the struct_time structure , one of whose members (
tm_yday
) is precisely the day of the year.Remarks :
print
have their argument enclosed in parentheses, to be compatible with Python 3.x.