python datetime time range

Solutions on MaxInterview for python datetime time range by the best coders in the world

showing results for - "python datetime time range"
Anton
15 Jul 2018
1import datetime
2ndays = 4
3now = datetime.datetime.today()
4date_list = [now - datetime.timedelta(days=x) for x in range(ndays)]
5date_list
6>>> [datetime.datetime(2021, 8, 6, 10, 5, 32, 676787), datetime.datetime(2021, 8, 5, 10, 5, 32, 676787), datetime.datetime(2021, 8, 4, 10, 5, 32, 676787), datetime.datetime(2021, 8, 3, 10, 5, 32, 676787)]
7
8# or 
9import pandas as pd
10import datetime
11datelist = pd.date_range(datetime.datetime.today(), periods=ndays)
12datelist
13>>> [Timestamp('2021-08-06 10:10:47.721405', freq='D'), Timestamp('2021-08-07 10:10:47.721405', freq='D'), Timestamp('2021-08-08 10:10:47.721405', freq='D'), Timestamp('2021-08-09 10:10:47.721405', freq='D')]
14