30秒学会 Python 片段 · 2022年9月24日

30秒学会 Python 片段 – Date range

Creates a list of dates between start (inclusive) and end (not inclusive).

  • Use datetime.timedelta.days to get the days between start and end.
  • Use int() to convert the result to an integer and range() to iterate over each day.
  • Use a list comprehension and datetime.timedelta to create a list of datetime.date objects.

代码实现

from datetime import timedelta, date

def daterange(start, end):
  return [start + timedelta(n) for n in range(int((end - start).days))]

使用样例

from datetime import date

daterange(date(2020, 10, 1), date(2020, 10, 5))
# [date(2020, 10, 1), date(2020, 10, 2), date(2020, 10, 3), date(2020, 10, 4)]

翻译自:https://www.30secondsofcode.org/python/s/daterange