Enumerate DateRange

I have 2 dates, and I'm trying to create the x-axis labels of the graph.

As such, I need a way to take 2 datetime objects, i.e. 2009-10-12 00:00:00 and 2009-10-20 00:00:00 and create a list like this:

["2009-10-12", "2009-10-13", "2009-10-14", ..., "2009-10-19", "2009-10-20"]

What libraries should I use to assist? I have a sense of datetime , and the timedelta strong> functionality will help a bit.

I can include the code if that makes sense, but I have the feeling that something is built into the python libraries that make it very easy. It seems I just missed it.

+3
source share
2
import datetime

first=datetime.date(2009,10,12)
last=datetime.date(2009,10,20)
adate=first
dates=[]
while adate<=last:
    dates.append(adate)
    adate+=datetime.timedelta(1)
print(dates)

, :

len=(last-first).days
dates=[first+datetime.timedelta(n) for n in range(len+1)]
+3
from datetime import date, timedelta

a, b = date(1010, 10, 12), date(1010, 10, 20)
times = [a + timedelta(x) for x in xrange((b-a).days)]

# If you want to format them:
times = [x.strftime('%Y-%m-%d') for x in times]
0

Source: https://habr.com/ru/post/1729979/


All Articles