Reputation: 22440
I've written a script to produce list of dates. The script I've tried with can do it. However, the way I need the list of dates is very different from what I'm getting at this moment.
The script I've tried with:
import datetime
start = datetime.datetime(2017,1,1)
end = datetime.datetime(2017,6,20)
daterange = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
for idate in daterange:
print(idate)
Current results are like:
2017-01-01 00:00:00
2017-01-02 00:00:00
2017-01-03 00:00:00
2017-01-04 00:00:00
2017-01-05 00:00:00
2017-01-06 00:00:00
The way I expect to get the dates like:
01-01-2017
01-02-2017
01-03-2017
01-04-2017
01-05-2017
01-06-2017
How can I modify my script to achieve the same? Thanks in advance.
Upvotes: 0
Views: 23
Reputation: 18208
You can try using strftime
in print
, for more details you can look into documentation:
print(idate.strftime("%d-%m-%Y"))
From documentation:
%d : Day of the month as a zero-padded decimal number.
%m : Month as a zero-padded decimal number.
%Y: Year with century as a decimal number.
For testing the code above:
import datetime
start = datetime.datetime(2017,1,1)
end = datetime.datetime(2017,6,20)
daterange = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
for idate in daterange:
print(idate.strftime("%d-%m-%Y"))
Result:
01-01-2017
02-01-2017
03-01-2017
04-01-2017
05-01-2017
06-01-2017
07-01-2017
08-01-2017
09-01-2017
.
.
.
Upvotes: 1