0

I want to list all dates and hours between 2 given inout datetimes using dateutil library.

Example: input: date_from= 02/16/2020:00:00:00, date_to = 02/17/2020:00:00:00

I want to have a list with the below structure

02/16/2020, 02/16/2020:00:00:00

02/16/2020, 02/16/2020:01:00:00

02/16/2020, 02/16/2020:02:00:00

... till hour 23

02/17/2020, 02/17/2020:00:00:00

02/17/2020, 02/17/2020:01:00:00

02/17/2020, 02/17/2020:02:00:00

.... till hour 23

I tried the below code and it seems the logic need to be changed.

days = [[], []]
for day in rrule.rrule(rrule.DAILY, dtstart=date_from, until=date_to):
    for hour in rrule.rrule(rrule.HOURLY, dtstart=date_from, until=date_to):
        days.append(str(day))
Mo T
  • 440
  • 2
  • 9
  • 30

2 Answers2

0
from datetime import datetime as dt
from datetime import timedelta

date_from= '02/16/2020 00:00:00'
date_to = '02/17/2020 00:00:00'

date_from = dt.strptime(date_from, '%m/%d/%Y %H:%M:%S')
date_to = dt.strptime(date_to, '%m/%d/%Y %H:%M:%S')
new_date = date_from
while new_date < date_to:
    print(new_date)
    new_date = new_date + timedelta(minutes=60)  

output:

2020-02-16 00:00:00
2020-02-16 01:00:00
2020-02-16 02:00:00
2020-02-16 03:00:00
2020-02-16 04:00:00
2020-02-16 05:00:00
2020-02-16 06:00:00
2020-02-16 07:00:00
2020-02-16 08:00:00
2020-02-16 09:00:00
2020-02-16 10:00:00
2020-02-16 11:00:00
2020-02-16 12:00:00
2020-02-16 13:00:00
2020-02-16 14:00:00
2020-02-16 15:00:00
2020-02-16 16:00:00
2020-02-16 17:00:00
2020-02-16 18:00:00
2020-02-16 19:00:00
2020-02-16 20:00:00
2020-02-16 21:00:00
2020-02-16 22:00:00
2020-02-16 23:00:00
Mayowa Ayodele
  • 549
  • 2
  • 11
0

As a 2D array of date and datetime:

from datetime import datetime as dt
from datetime import timedelta

date_from= '02/16/2020 00:00:00'
date_to = '02/17/2020 00:00:00'

date_from = dt.strptime(date_from, '%m/%d/%Y %H:%M:%S')
date_to = dt.strptime(date_to, '%m/%d/%Y %H:%M:%S')
new_date = date_from
all_dates = []
while new_date < date_to:
    date_only = dt.strftime(new_date,'%m/%d/%Y')
    date_and_time = dt.strftime(new_date,'%m/%d/%Y %H:%M:%S')
    all_dates.append([date_only,date_and_time])
    new_date = new_date + timedelta(minutes=60)  

print(all_dates)

output:

[['02/16/2020', '02/16/2020 00:00:00'], ['02/16/2020', '02/16/2020 01:00:00'], ['02/16/2020', '02/16/2020 02:00:00'], ['02/16/2020', '02/16/2020 03:00:00'], ['02/16/2020', '02/16/2020 04:00:00'], ['02/16/2020', '02/16/2020 05:00:00'], ['02/16/2020', '02/16/2020 06:00:00'], ['02/16/2020', '02/16/2020 07:00:00'], ['02/16/2020', '02/16/2020 08:00:00'], ['02/16/2020', '02/16/2020 09:00:00'], ['02/16/2020', '02/16/2020 10:00:00'], ['02/16/2020', '02/16/2020 11:00:00'], ['02/16/2020', '02/16/2020 12:00:00'], ['02/16/2020', '02/16/2020 13:00:00'], ['02/16/2020', '02/16/2020 14:00:00'], ['02/16/2020', '02/16/2020 15:00:00'], ['02/16/2020', '02/16/2020 16:00:00'], ['02/16/2020', '02/16/2020 17:00:00'], ['02/16/2020', '02/16/2020 18:00:00'], ['02/16/2020', '02/16/2020 19:00:00'], ['02/16/2020', '02/16/2020 20:00:00'], ['02/16/2020', '02/16/2020 21:00:00'], ['02/16/2020', '02/16/2020 22:00:00'], ['02/16/2020', '02/16/2020 23:00:00']]

Is this what you want?

Mayowa Ayodele
  • 549
  • 2
  • 11