-3

I want to iterate through the dates in for loop. sample ddates : 20170101 20170107

output should be : 20170101 20170102 20170103 20170104 20170105 20170106 20170107

3
  • Wheres your code? Commented Oct 25, 2017 at 11:33
  • for i in range(20170101,20170107 ): print i Commented Oct 25, 2017 at 11:33
  • It is taking the dates as integer. I want to convert it to date format Commented Oct 25, 2017 at 11:36

1 Answer 1

-1

datetime.strptime is what you're looking for

start = 20170101
end = 20170107
dates = []
for i in range(start, end):
  dates.append(datetime.strptime(str(i), "%Y%m%d"))

dates will then have:

[datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2017, 1, 2, 0, 0), datetime.datetime(2017, 1, 3, 0, 0), datetime.datetime(2017, 1, 4, 0, 0), datetime.datetime(2017, 1, 5, 0, 0), datetime.datetime(2017, 1, 6, 0, 0)]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.