How to calculate number of days between two given dates?

前端 未结 13 1469
梦如初夏
梦如初夏 2020-11-22 06:29

If I have two dates (ex. \'8/18/2008\' and \'9/26/2008\'), what is the best way to get the number of days between these two dates?

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 06:45

    Here are three ways to go with this problem :

    from datetime import datetime
    
    Now = datetime.now()
    StartDate = datetime.strptime(str(Now.year) +'-01-01', '%Y-%m-%d')
    NumberOfDays = (Now - StartDate)
    
    print(NumberOfDays.days)                     # Starts at 0
    print(datetime.now().timetuple().tm_yday)    # Starts at 1
    print(Now.strftime('%j'))                    # Starts at 1
    

提交回复
热议问题