Why is Python datetime time delta not found?

前端 未结 3 1582
长发绾君心
长发绾君心 2020-12-17 09:40

I am trying to make an array of dates in mmddyyyy format. The dates will start on the current day and then go two weeks into the future. So it all depends on the starting da

3条回答
  •  孤城傲影
    2020-12-17 10:26

    import time
    from datetime import datetime, date, time, timedelta
    
    dayDates = []
    today = datetime.now()
    dayDates.append(today.strftime("%m%d%Y"))
    for i in range(0,14):
        day = today + datetime.timedelta(days=i)
        print day
    

    The error that you are getting says, that datetime has no attribute timedelta. It happens, because you have imported from datetime specific things. In order to access timedelta now you type timedelta instead of datetime.timedelta.

    import time
    from datetime import datetime, date, time, timedelta
    
    dayDates = []
    today = datetime.now()
    dayDates.append(today.strftime("%m%d%Y"))
    for i in range(0,14):
        day = today + timedelta(days=i)
        print day
    

    Like that, your code should work properly. Also, pay closer attention to the error messages and try to read them carefully. If you focus enough, you often can sort out the problem basing on them on your own.

提交回复
热议问题