Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a loop for two dates [duplicate]

Possible Duplicate:
Iterating through a range of dates in Python

I have two entries:

date1 = 2004.09.25
date2 = 2004.10.08

I want to wrote a script in Python that recognizes the date range and print them. something like this:

for i in range(date1:date2):
    print i

Do I need to define the dates in a particular format for dates? The expected output is like:

2004.09.25
2004.09.26
.
.
.
2004.10.08
like image 409
f.ashouri Avatar asked Jan 11 '13 23:01

f.ashouri


People also ask

How do I loop through a date range?

One way to loop through a date range with JavaScript is to use a while loop. We can create variables for the start and end dates. Then we can increment the start date until it reaches the end date. We have the start and end variables with the start and end date respectively.

How do you run a loop in Python?

To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.


2 Answers

Your first step should have been to look at the python datetime library.

Overall, your first solution could look something like this:

date1 = datetime.date(2004, 9, 25)
date2 = datetime.date(2004, 10, 8)
day = datetime.timedelta(days=1)

while date1 <= date2:
    print date1.strftime('%Y.%m.%d')
    date1 = date1 + day

(one thing to note: this will obviously clobber your date1 variable)

I would later refactor this into a daterange function so that you can do something closer to what you did; it would look like

for d in daterange(date1, date2):
    print d.strftime('%Y.%m.%d')

Later on, when you develop your python skills, it could like like this:

for i in range((date2 - date1).days + 1):
    print (date1 + datetime.timedelta(days=i)).strftime('%Y.%m.%d')

Or this, which would be my final version:

def daterange(d1, d2):
    return (d1 + datetime.timedelta(days=i) for i in range((d2 - d1).days + 1))

for d in daterange(date1, date2):
    print d.strftime('%Y.%m.%d')
like image 112
forivall Avatar answered Sep 20 '22 04:09

forivall


Use the builtin datetime module.

Firstly, split your dates (assuming it is a string) by each . and pass the lists to datetime.date

date1 = datetime.date(*date1.split('.'))
date2 = datetime.date(*date2.split('.'))

Then get the ordinal of each of the dates (number of days since Jan 1 0001)

date1 = date1.toordinal()
date2 = date2.toordinal()

Then, use a for loop, and the fromordinal function to convert an ordinal to a date

for i in range(date1, date2 + 1):
    print date.fromordinal(i).strftime('%Y.%m.%d')

Putting it all together:

date1 = datetime.date(*date1.split('.')).toordinal()
date2 = datetime.date(*date2.split('.')).toordinal()
for i in range(date1, date2 + 1):
    print date.fromordinal(i).strftime('%Y.%m.%d')
like image 35
Volatility Avatar answered Sep 19 '22 04:09

Volatility