Python generating a list of dates between two dates

python datetime
python days between two dates
print all months between two dates in python
python get list of weekdays between two dates
python date range
python build list of dates
random date between two dates python
get data between two dates in python

I want to generate a list of dates between two dates and store them in a list in string format. This list is useful to compare with other dates I have.

My code is given below:

from datetime import date, timedelta

sdate = date(2019,3,22)   # start date
edate = date(2019,4,9)   # end date

def dates_bwn_twodates(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)
print(dates_bwn_twodates(sdate,edate))

My present output:

<generator object dates_bwn_twodates at 0x000002A8E7929410>

My expected output:

['2019-03-22',.....,'2019-04-08']

Something wrong in my code.

You can use pd.date_range() for this:

pd.date_range(sdate,edate-timedelta(days=1),freq='d')

DatetimeIndex(['2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25',
           '2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29',
           '2019-03-30', '2019-03-31', '2019-04-01', '2019-04-02',
           '2019-04-03', '2019-04-04', '2019-04-05', '2019-04-06',
           '2019-04-07', '2019-04-08'],
          dtype='datetime64[ns]', freq='D')

Print all day-dates between two dates, I came up with this: from datetime import date, timedelta sdate = date(2008, 8, 15) # start date edate = date(2008, 9, 15) # end date delta = edate - sdate # as� Write a Python program to get a list of dates between two dates. Sample Solution: Python Code: from datetime import timedelta, datedef 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"))

You'd need to turn it into a list with strings explicitly:

print([str(d) for d in dates_bwn_twodates(sdate,edate)])

Python Exercise: Get a list of dates between two dates, Next: Write a Python program to generate RFC 3339 timestamp. What is the difficulty level of this exercise? Easy Medium Hard. Test� Kite is a free autocomplete for Python developers. Code faster with the Kite plugin for your code editor, featuring Line-of-Code Completions and cloudless processing.

from datetime import date, timedelta

sdate = date(2019,3,22)   # start date
edate = date(2019,4,9)   # end date
date_modified=sdate
list=[sdate] 


while date_modified<edate:
    date_modified+=timedelta(days=nbDaysbtw2dates) 
    list.append(date_modified)

print(list) 

datetime - Get all dates between two dates, Python code example 'Get all dates between two dates' for the package datetime, powered by Kite. Create a datetime object for the start and end dates. The following determines the number of days: Expand | Select | Wrap | Line Numbers. (end_date+datetime.timedelta (days=1)-start_date).days) Use that value +1 for a range of days.

How to generate a random date between two dates in Python, Kite is a free autocomplete for Python developers. Code faster with the Kite plugin for your code editor, featuring Line-of-Code Completions and cloudless� I want to generate a python list containing all months occurring between two dates, with the input and output formatted as follows: date1 = "2014-10-10" # input start date date2 = "2016-01-07" #

How to create list of dates between two dates? - Python, Hello, I am a beginner in python, and have a question.. I need to create a list of all dates between two dates.. Lets say beggining date= 2008-1-� Get range of dates between specified start and end date in python - PythonProgramming.in Get range of dates between specified start and end date This is a short program using timedelta to print all range of dates between two specific dates.

Get range of dates between specified start and end date in python , # Python's program to display all dates between two dates. import datetime. start = datetime.datetime.strptime(� start_date = datetime.now() end_date = start_date + relativedelta(days=1) end_date = end_date.replace(hour=19, minute=0, second=0, microsecond=0) date_range(start_date, end_date, 8, 'hours')

Comments
  • are you looking for pd.date_range(sdate,edate-timedelta(days=1),freq='d') ?
  • Using yield means that your function will return a generator. If you want to run the generator to get all of the elements, you can do print(list(dates_bwn_twodates(sdate, edate))).
  • @anky_91 Yes! It worked perfectly.
  • @anky_91 I need advise. If I want to check if a date is in above list? how to compare with a list?
  • @Mainland Is your goal to check if a date is between two other dates? If so, the list is unnecessary and there are simpler ways to accomplish that check
  • I need advise. If I want to check if a date is in above list? how to compare with a list?
  • @Mainland something like : idx = pd.date_range(sdate,edate-timedelta(days=1),freq='d') , idx.isin(['2019-03-22', '2019-03-23']) ?
  • Yes! this is what I wanted. On top of the that I prefer .any() at end to get a simple True or False but not a list.
  • How to convert '2019-03-22' into a datetime format?
  • @Mainland pd.to_datetime('2019-03-22').date()