30秒学会 Python 片段 · 2023年3月31日

30秒学会 Python 片段 – Date difference in days

Calculates the day difference between two dates.

  • Subtract start from end and use datetime.timedelta.days to get the day difference.

代码实现

def days_diff(start, end):
  return (end - start).days

使用样例

from datetime import date

days_diff(date(2020, 10, 25), date(2020, 10, 28)) # 3

翻译自:https://www.30secondsofcode.org/python/s/days-diff