How do i get all the dates between two dates in python?

I'm surprised this isn't a standard function in datetime package.

Here's a function that does what is requested:

from datetime import timedelta

def date_range_list(start_date, end_date):
    # Return list of datetime.date objects between start_date and end_date (inclusive).
    date_list = []
    curr_date = start_date
    while curr_date <= end_date:
        date_list.append(curr_date)
        curr_date += timedelta(days=1)
    return date_list

Usage:

from datetime import date, timedelta

def date_range_list(start_date, end_date):
    # Return list of datetime.date objects between start_date and end_date (inclusive).
    date_list = []
    curr_date = start_date
    while curr_date <= end_date:
        date_list.append(curr_date)
        curr_date += timedelta(days=1)
    return date_list

start_date = datetime.date(year=2021, month=12, day=20)
stop_date = datetime.date(year=2021, month=12, day=25)
date_list = date_range_list(start_date, stop_date)

date_list

Output:

[datetime.date(2021, 12, 20),
 datetime.date(2021, 12, 21),
 datetime.date(2021, 12, 22),
 datetime.date(2021, 12, 23),
 datetime.date(2021, 12, 24),
 datetime.date(2021, 12, 25)]

Last update on August 19 2022 21:51:51 (UTC/GMT +8 hours)

Python Datetime: Exercise-50 with Solution

Write a Python program to get a list of dates between two dates.

Sample Solution:

Python Code:

from datetime import timedelta, date

def daterange(date1, date2):
    for n in range(int ((date2 - date1).days)+1):
        yield date1 + timedelta(n)

start_dt = date(2015, 12, 20)
end_dt = date(2016, 1, 11)
for dt in daterange(start_dt, end_dt):
    print(dt.strftime("%Y-%m-%d"))
	

Sample Output:

2015-12-20                                                                                                    
2015-12-21                                                                                                    
2015-12-22                                                                                                    
2015-12-23                                                                                                    
2015-12-24                                                                                                    
2015-12-25                                                                                                    
2015-12-26                                                                                                    
2015-12-27                                                                                                    
2015-12-28                                                                                                    
2015-12-29                                                                                                    
2015-12-30                                                                                                    
2015-12-31   
-------
2016-01-08                                                                                                    
2016-01-09                                                                                                    
2016-01-10                                                                                                    
2016-01-11 

Flowchart:

How do i get all the dates between two dates in python?

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to convert a string into datetime.
Next: Write a Python program to generate RFC 3339 timestamp.

Python: Tips of the Day

Checking whether all elements in the sequence are Truthful:

>>> all(a % 2==0 for a in range(0,10,2))
True

June 28, 2022 Category : Python

In this article we will cover on how to implement python get dates between two dates. In this article, we will implement a python get all dates between two dates. I would like to show you how to get all dates between two dates in python. We will look at example of python list all dates between two dates. Follow bellow tutorial step of python get all dates between two datetime.

In this example, I will give two examples one example will get all dates between two dates using datetime and timedelta, and the second example find all dates between two dates using pandas. so let's see both examples and use them for you.

You can use these examples with python3 (Python 3) version.

Example 1:

main.py

from datetime import datetime, timedelta
  
# Create Custom Function
def date_range(start, end):
    delta = end - start
    days = [start + timedelta(days=i) for i in range(delta.days + 1)]
    return days
  
startDate = datetime(2022, 6, 1)
endDate = datetime(2022, 6, 10)
      
datesRange = date_range(startDate, endDate);
print(datesRange)

Output:

[
    datetime.datetime(2022, 6, 1, 0, 0), 
    datetime.datetime(2022, 6, 2, 0, 0), 
    datetime.datetime(2022, 6, 3, 0, 0), 
    datetime.datetime(2022, 6, 4, 0, 0), 
    datetime.datetime(2022, 6, 5, 0, 0), 
    datetime.datetime(2022, 6, 6, 0, 0), 
    datetime.datetime(2022, 6, 7, 0, 0), 
    datetime.datetime(2022, 6, 8, 0, 0), 
    datetime.datetime(2022, 6, 9, 0, 0), 
    datetime.datetime(2022, 6, 10, 0, 0)
]

Example 2:

main.py

import pandas
from datetime import datetime, timedelta
  
startDate = datetime(2022, 6, 1)
endDate = datetime(2022, 6, 10)
  
# Getting List of Days using pandas
datesRange = pandas.date_range(startDate,endDate-timedelta(days=1),freq='d')
print(datesRange);

Output:

 
DatetimeIndex(['2022-06-01', '2022-06-02', '2022-06-03', '2022-06-04',
               '2022-06-05', '2022-06-06', '2022-06-07', '2022-06-08',
               '2022-06-09'],
              dtype='datetime64[ns]', freq='D')

I hope it can help you...

How do i get all the dates between two dates in python?

Hardik Savani

I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

Follow Me:

We are Recommending you

  • How to Check if Today is Sunday or not in Python?
  • How to Check if Today is Tuesday or not in Python?
  • Python Subtract Seconds from DateTime Example
  • How to Add Minutes to DateTime in Python?
  • Python DELETE Request Example Tutorial
  • Python POST Request with Parameters Example
  • Python Subtract Hours from Datetime Example
  • How to Subtract Year to Date in Python?
  • How to Subtract Months to Date in Python?
  • How to Subtract Days from Date in Python?
  • How to Get Current Month in Python?

How do I generate all dates between two dates in Python?

import pandas from datetime import datetime, timedelta startDate = datetime(2022, 6, 1) endDate = datetime(2022, 6, 10) # Getting List of Days using pandas datesRange = pandas. date_range(startDate,endDate-timedelta(days=1),freq='d') print(datesRange);

How can I get a list of dates between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

How do I search between dates in Python?

if date in (start, end): print('in between') else: print('No! ').
I think this is exactly what i want. i have them converted to int and add to tuple. ... .
Just remember that doing it this way, you can't be sure if they are real dates. ... .
I got the date data from my table. ... .
Note that it can be used only for dates of one year!.

How do you iterate over a date range in Python?

How to iterate through a range of dates in Python.
start_date = datetime. date(2020, 1, 1).
end_date = datetime. date(2020, 1, 4).
delta = datetime. timedelta(days=1).
while start_date <= end_date:.
print(start_date).
start_date += delta..