[Python] Create a list of date and time (datetime type) for a certain period


Input and output

Code used

import datetime

def list_date(start_date='2020-01-01', end_date='2020-01-31'):

    #Convert str type to datetime type.
    date_start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    date_end = datetime.datetime.strptime(end_date, '%Y-%m-%d')

    #Calculate the interval between the start date and the end date.
    days = (date_end - date_start).days

    #The for statement is rotated by the interval and the day is added.
    tmp_date = start_date
    date = [tmp_date]
    for i in range(days):
        tmp_date += datetime.timedelta(days=1)

    return date

Example of use

date = list_date(start_date='2020-01-01', end_date='2020-01-05')

Execution result

[datetime.datetime(2020 1, 1, 0, 0),
 datetime.datetime(2020 1, 2, 0, 0),
 datetime.datetime(2020 1, 3, 0, 0),
 datetime.datetime(2020 1, 4, 0, 0),
 datetime.datetime(2020 1, 5, 0, 0)]

Recommended Posts

[Python] Create a list of date and time (datetime type) for a certain period
[Python] Create a date and time list for a specified period
Python: Create a dictionary from a list of keys and values
Put the process to sleep for a certain period of time (seconds) or more in Python
Randomly play the movie on ChromeCast for a certain period of time
[python] Create a list of various character types
[Python] Class type and usage of datetime module
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Extract "current date only" and "current date and time" with python datetime.
List of Python libraries for data scientists and data engineers
How to write a list / dictionary type of Python3
Python: Get a list of methods for an object
Summary of date processing in Python (datetime and dateutil)
Pandas basics for beginners ④ Handling of date and time items
Spit out a list of file name, last modified date and character code in python3
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Build and test a CI environment for multiple versions of Python
Create a striped illusion with gamma correction for Python3 and openCV3
I tried to create a list of prime numbers with python
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)
Dig the directory and create a list of directory paths + file names
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
python memo: enumerate () -get index and element of list at the same time and turn for statement
For Windows: Get a list of directories and files under a specific directory.
Conversion of string <-> date (date, datetime) in Python
Challenge to create time axis list report with Toggl API and Python
Display a list of alphabets in Python 3
Let's create a virtual environment for Python
Connect a lot of Python or and and
Receive date type (datetime) with ArgumentParser [python]
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Get a datetime instance at any time of the day in Python
I measured the speed of list comprehension, for and while with python2.7.
[Python] Get a list of folders only
How to stop a program in python until a specific date and time
How to use the Slack API using Python to delete messages that have passed a certain period of time for a specific user on a specific channel
Experiment to collect tweets for a long period of time (aggregation & content confirmation)
Python script to get a list of input examples for the AtCoder contest
[Python] List Comprehension Various ways to create a list
Convert "number" of excel date to python datetime
Create a LINE BOT with Minette for Python
Make a copy of the list in Python
Create a web map using Python and GDAL
Create a datetime object from a string in Python (Python 3.3)
List of Python code to move and remember
Create a Mac app using py2app and Python3! !!
[Python] A rough understanding of iterators, iterators, and generators
A discussion of the strengths and weaknesses of Python
To represent date, time, time, and seconds in Python
[Python] Convert time display (str type) using "" "and"'" to seconds (float type) with datetime and timedelta
Get the value of a specific key in a list from the dictionary type in the list with Python
[Python] Rename all image files in a specific folder by shooting date and time
Check the processing time and the number of calls for each process in python (cProfile)
Create a dataset of images to use for learning
Convert timezoned date and time to Unixtime in Python2.7
Prepared for date calculation and automation of my bot
Building a Docker working environment for R and Python
[Python] Measures and displays the time required for processing