[Python] Create a date and time list for a specified period

Thing you want to do

I want to create a list with 6-hour intervals from 05:00 on October 29, 2020 to 00:00 on November 1, 2020.

python code

from datetime import datetime, timedelta

def perdate(start, end, delta):
    current = start
    while current < end:
     yield current
     current += delta

for result in perdate(datetime(2020, 10, 29, 5,0,0), datetime(2020, 11, 1, 0,0,0), timedelta(hours=6)):
    print (result)

Execution result

2020-10-29 05:00:00
2020-10-29 11:00:00
2020-10-29 17:00:00
2020-10-29 23:00:00
2020-10-30 05:00:00
2020-10-30 11:00:00
2020-10-30 17:00:00
2020-10-30 23:00:00
2020-10-31 05:00:00
2020-10-31 11:00:00
2020-10-31 17:00:00
2020-10-31 23:00:00

Reference article

Recommended Posts

[Python] Create a date and time list for a specified period
Python: Create a dictionary from a list of keys and values
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Create a striped illusion with gamma correction for Python3 and openCV3
Python vba to create a date string for creating a file name
Create a CGH for branching a laser in Python (laser and SLM required)
Get date and time in specified format
Let's create a virtual environment for Python
Challenge to create time axis list report with Toggl API and Python
How to stop a program in python until a specific date and time
[Python] List Comprehension Various ways to create a list
[python] Create a list of various character types
Create a LINE BOT with Minette for Python
Create a web map using Python and GDAL
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Create a Mac app using py2app and Python3! !!
Sort list elements in a specified order in Python
To represent date, time, time, and seconds in Python
Create a Python module
Create a Python environment
Convert timezoned date and time to Unixtime in Python2.7
Extract "current date only" and "current date and time" with python datetime.
List of Python libraries for data scientists and data engineers
Stop EC2 for specified time + start with Lambda (python)
[Python] Measures and displays the time required for processing
[Python] 2 Create a risk-return map for your asset portfolio
Python 3.4 Create Windows7-64bit environment (for financial time series analysis)
Create a Layer for AWS Lambda Python with Docker
[python] Create a date array with arbitrary increments with np.arange
Python: Get a list of methods for an object
Create a decent shell and python environment on Windows
(For myself) Flask_2 (list and for, extends, and a little more)
[Python] Rename all image files in a specific folder by shooting date and time
Python list, for statement, dictionary
Create a dictionary in Python
List method argument information for classes and modules in Python
Spit out a list of file name, last modified date and character code in python3
Create ToDo List [Python Django]
[Python] Create a linebot that draws any date on a photo
Create a child account for connect with Stripe in Python
Let's create a PRML diagram with Python, Numpy and matplotlib.
Installation procedure for Python and Ansible with a specific version
Date and time ⇔ character string
Python list is not a list
Useful tricks related to list and for statements in Python
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a python numpy array
Create code that outputs "A and pretending B" in python
Get the current date and time in Python, considering the time difference
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
Pandas basics for beginners ④ Handling of date and time items
Python list and tuples and commas
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
Create and list Django models
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Python #list for super beginners
Library for specifying a name server and dig with python
Create a directory with python
Create a virtual environment for python on mac [Very easy]
I want to create a Dockerfile for the time being.