I enter date_1 = 2016-10-12 , then to subtract 1 day from it and get the date 2016-10-11. Why does the code give the error Required argument 'month' (pos 2) not found ?

 import datetime import time date_1 = 2016-10-12 ds = datetime.date(date_1) delta = datetime.timedelta(days=1) # дельта в 1 день now_date = ds - delta print(now_date) 

    1 answer 1

    Because when you write 2016-10-12, the interpreter thinks that this is "2016 minus 10 minus 12". He performs these calculations, and gets the number 1994. And the datetime.date requires that he be given three separate numbers - year, month, day. If he gets only one number, then he takes it as a year, but he does not find the month anymore, and he writes about the error in the text.

    However, this can be done almost the way you tried, if you use the unpacking syntax:

     date_1 = 2016, 10, 12 ds = datetime.date(*date_1) 
    • if the date in the form of a string is set: datestr = "2016-10-12" , then d = DT.datetime.strptime(datestr, '%Y-%m-%d').date() - jfs