• Post author:
  • Post category:Python
  • Reading time:2 mins read

Here is a Python program to get date range between two dates. Also, you can specify the date incremental value. For example, specify step value 2 to get the date range by incrementing the date with two days.

Get The Date Range Between Two Dates in Python

In the below Python program, it will get the date range from 1st Jan 2019 to 11th Jan 2019, incrementing by one day.

from datetime import datetime, date, timedelta
import calendar

def get_daterange(start_date, end_date, step):
    while start_date <= end_date:
        yield start_date
        start_date += step

for d in get_daterange(date(2019, 1, 1), date(2019, 1, 11), timedelta(days=1)):
    print(d)

Output:

2019-01-01
2019-01-02
2019-01-03
2019-01-04
2019-01-05
2019-01-06
2019-01-07
2019-01-08
2019-01-09
2019-01-10
2019-01-11

Process finished with exit code 0

In the following Python program, it will get the date range from 19th Jan 2019 to 31st Jan 2019, incrementing by two days.

from datetime import datetime, date, timedelta
import calendar

def get_daterange(start_date, end_date, step):
    while start_date <= end_date:
        yield start_date
        start_date += step

for d in get_daterange(date(2019, 1, 19), date(2019, 1, 31), timedelta(days=2)):
    print(d)

Output:

2019-01-19
2019-01-21
2019-01-23
2019-01-25
2019-01-27
2019-01-29
2019-01-31

Process finished with exit code 0

See also:

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of foxinfotech.in and orclqa.com a question and answer forum for developers.