Reputation: 184
Year = eval(input("Year:"))
import time
import datetime
from datetime import datetime.timedelta
t1 = datetime.datetime(Year,12,31).strftime("%m%d.%w")
t2 = datetime.datetime(Year,12,31).strftime("%j")
t3 = datetime.datetime(Year,12,31).strftime("%d")
t4 = timedelta(days=-1)
t5 = t1 - t4
I'm not good at English. I want to decrease the days and the first one is 1231 in any years. how can I decrease the days by "for loop" , the first output is 1231 , the second one is 1230 ,and the third one is 1229 .....until 0101 ?
I do not really understand the meaning of "timedelta" and "datetime".But I can't pretty understand the information of the web on python official web.
Upvotes: 1
Views: 431
Reputation: 1568
Do you mean something like this?
import datetime
Year = 2014
begin = datetime.date(Year,01,01)
end = datetime.date(Year,12,31)
for d in range((end - begin).days + 1):
print(end - datetime.timedelta(days=d))
Or did I misunderstand your question?
Upvotes: 0
Reputation: 473
You can try this :
>>> import datetime
>>> def back_in_the_past(start_year):
... start_date = datetime.datetime(start_year, 12, 31)
... for i in range(0, 5):
... new_date = start_date - datetime.timedelta(i)
... print(new_date.strftime("%Y-%m-%d"))
...
>>> back_in_the_past(2014)
2014-12-31
2014-12-30
2014-12-29
2014-12-28
2014-12-27
You just have to change "for i in range(0, 5):" to back in time for more days.
datetime.timedelta(i) apply a translation in time - the first argument is in days, so "i" is in days.
Upvotes: 2