How to calculate number of days between two given dates?

前端 未结 13 1290
梦如初夏
梦如初夏 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:55

    from datetime import date
    def d(s):
      [month, day, year] = map(int, s.split('/'))
      return date(year, month, day)
    def days(start, end):
      return (d(end) - d(start)).days
    print days('8/18/2008', '9/26/2008')
    

    This assumes, of course, that you've already verified that your dates are in the format r'\d+/\d+/\d+'.

提交回复
热议问题