Tuesday 22 February 2022

How to get all dates (month, day and year) between two dates in python?

 You don't have to reinvent the wheel. Just parse the strings into datetime objects and let python do the math for you:

from dateutil import rrule
from datetime import datetime

a = '20120525'
b = '20120627'

for dt in rrule.rrule(rrule.DAILY,
                      dtstart=datetime.strptime(a, '%Y%m%d'),
                      until=datetime.strptime(b, '%Y%m%d')):
    print dt.strftime('%Y%m%d')

prints

20120525
20120526
2012052720120625
20120626
20120627


from: https://stackoverflow.com/questions/11317378/how-to-get-all-dates-month-day-and-year-between-two-dates-in-python

No comments:

Post a Comment