Assume that you want to generate a date series in Python having as input the start and the end date. Let’s see how we can achieve that.
import datetime # define the start and the end date sdate = '2023-09-10' edate = '2023-09-14' # define the date format date_format = '%Y-%m-%d' # convert the string objects to datetime and then to dates sdate = datetime.datetime.strptime(sdate, date_format).date() edate = datetime.datetime.strptime(edate, date_format).date() # using list comprehension generate the list of dates date_lst = [str(sdate+datetime.timedelta(days=x)) for x in range((edate-sdate).days)] print(date_lst)
Output:
['2023-09-10', '2023-09-11', '2023-09-12', '2023-09-13']