Python> datetime> Implementation> Specify the end date, interval, and start date, and return and display each interval.

Operation check


Python 3 (ideone)

Implement the following.

--Start from the end date --Return the date every interval --Displays the date beyond the start date and stops

It seems to use timedelta () of datetime. https://docs.python.jp/3/library/datetime.html

code

http://ideone.com/maiFmH

from datetime import datetime as dt
from datetime import timedelta

ENDSTR = "20170202"
STARTSTR = "20161031"
INTERVAL_DAY = 13

enddt = dt.strptime(ENDSTR, '%Y%m%d')
startdt = dt.strptime(STARTSTR, '%Y%m%d')

wrkdt = enddt
while True:
    print(wrkdt)
    if wrkdt < startdt:
        break
    wrkdt = wrkdt + timedelta(days=-INTERVAL_DAY)

run


2017-02-02 00:00:00
2017-01-20 00:00:00
2017-01-07 00:00:00
2016-12-25 00:00:00
2016-12-12 00:00:00
2016-11-29 00:00:00
2016-11-16 00:00:00
2016-11-03 00:00:00
2016-10-21 00:00:00

Recommended Posts

Python> datetime> Implementation> Specify the end date, interval, and start date, and return and display each interval.
[Python] How to specify the window display position and size of matplotlib
Specify the start and end positions of files to be included with qiitap
Specify the Django http status code and return.
[Python] How to change the date format (display format)
Extract "current date only" and "current date and time" with python datetime.
Install django on python + anaconda and start the server
Summary of date processing in Python (datetime and dateutil)
[Python] Split the date
[Python] Read the csv file and display the figure with matplotlib
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Get the current date and time in Python, considering the time difference