2

Suppose I have two dates. How can I get all the intermediate dates and hours (from 00 to 23) between the two dates? Thank you very much!

from datetime import datetime,timedelta, date

date_min = (datetime.today()-timedelta(1)).strftime('%Y-%m-%d')+' 00'
date_max =  datetime.today().strftime('%Y-%m-%d')+' 00'

date_min, date_max

('2018-09-16 00', '2018-09-17 00')

how to obtain?

'2018-09-16 01', '2018-09-16 02', '2018-09-16 03', '2018-09-16 04', .... , '2018-09-16 21', '2018-09-16 22', '2018-09-16 23', '2018-09-17 00'
1
  • So you're looking for every day between date_min and date_max alongside every hour for each day? It sounds like you'd need a for loop at the least. Are you looking to do this entirely in datetime? Or would a solution using pandas like the one below work? Commented Sep 17, 2018 at 20:45

3 Answers 3

4

Use pandas, it's a one-liner:

import pandas as pd
solution = pd.date_range(start=date_min , end=date_max, freq='1H')
print(solution)    
DatetimeIndex(['2018-09-16 00:00:00', '2018-09-16 01:00:00',
                   '2018-09-16 02:00:00', '2018-09-16 03:00:00',
                   '2018-09-16 04:00:00', '2018-09-16 05:00:00',
                   '2018-09-16 06:00:00', '2018-09-16 07:00:00',
                   '2018-09-16 08:00:00', '2018-09-16 09:00:00',
                   '2018-09-16 10:00:00', '2018-09-16 11:00:00',
                   '2018-09-16 12:00:00', '2018-09-16 13:00:00',
                   '2018-09-16 14:00:00', '2018-09-16 15:00:00',
                   '2018-09-16 16:00:00', '2018-09-16 17:00:00',
                   '2018-09-16 18:00:00', '2018-09-16 19:00:00',
                   '2018-09-16 20:00:00', '2018-09-16 21:00:00',
                   '2018-09-16 22:00:00', '2018-09-16 23:00:00',
                   '2018-09-17 00:00:00'],
                  dtype='datetime64[ns]', freq='H')

If you want to convert from pandas timestamp back to datetime, do:

[timestamp.to_pydatetime() for timestamp in solution]

[datetime.datetime(2018, 9, 16, 0, 0), datetime.datetime(2018, 9, 16, 1, 0), datetime.datetime(2018, 9, 16, 2, 0), datetime.datetime(2018, 9, 16, 3, 0), datetime.datetime(2018, 9, 16, 4, 0), datetime.datetime(2018, 9, 16, 5, 0), datetime.datetime(2018, 9, 16, 6, 0), datetime.datetime(2018, 9, 16, 7, 0), datetime.datetime(2018, 9, 16, 8, 0), datetime.datetime(2018, 9, 16, 9, 0), datetime.datetime(2018, 9, 16, 10, 0), datetime.datetime(2018, 9, 16, 11, 0), datetime.datetime(2018, 9, 16, 12, 0), datetime.datetime(2018, 9, 16, 13, 0), datetime.datetime(2018, 9, 16, 14, 0), datetime.datetime(2018, 9, 16, 15, 0), datetime.datetime(2018, 9, 16, 16, 0), datetime.datetime(2018, 9, 16, 17, 0), datetime.datetime(2018, 9, 16, 18, 0), datetime.datetime(2018, 9, 16, 19, 0), datetime.datetime(2018, 9, 16, 20, 0), datetime.datetime(2018, 9, 16, 21, 0), datetime.datetime(2018, 9, 16, 22, 0), datetime.datetime(2018, 9, 16, 23, 0), datetime.datetime(2018, 9, 17, 0, 0)]

To convert to the string format you mentioned:

[timestamp.to_pydatetime().strftime('%Y-%m-%d %H') for timestamp in solution]

['2018-09-16 00', '2018-09-16 01', '2018-09-16 02', '2018-09-16 03', '2018-09-16 04', '2018-09-16 05', '2018-09-16 06', '2018-09-16 07', '2018-09-16 08', '2018-09-16 09', '2018-09-16 10', '2018-09-16 11', '2018-09-16 12', '2018-09-16 13', '2018-09-16 14', '2018-09-16 15', '2018-09-16 16', '2018-09-16 17', '2018-09-16 18', '2018-09-16 19', '2018-09-16 20', '2018-09-16 21', '2018-09-16 22', '2018-09-16 23', '2018-09-17 00']
Sign up to request clarification or add additional context in comments.

Comments

3

This will work:

date = datetime.today()-timedelta(days=1)
while date < datetime.today():
    date = date + timedelta(hours=1)
    print date.strftime('%Y-%m-%d %H')

and give you the following Output:

2018-09-17 00
2018-09-17 01
2018-09-17 02
2018-09-17 03
2018-09-17 04
2018-09-17 05
2018-09-17 06
2018-09-17 07
2018-09-17 08

You can also specify from which hour to start:

date = datetime.today().replace(hour=0, minute=0, second=0)-timedelta(days=1)

2 Comments

And what about if I want to pass the variables date_min = (datetime.today()-timedelta(1)).strftime('%Y-%m-%d')+' 00' date_max = datetime.today().strftime('%Y-%m-%d')+' 00'
while date_min < date_max: date = date + timedelta(hours=1) print(date.strftime('%Y-%m-%d %H'))
2

Pure Python:

import itertools
from datetime import datetime,timedelta

date_max = datetime.today()
date_min = date_max - timedelta(hours=2)
n_hours = (date_max - date_min).total_seconds() / 3600
[date_min + timedelta(hours=x) for x in itertools.takewhile(lambda h: h <= n_hours, itertools.count())]

returns

[datetime.datetime(2018, 9, 17, 14, 49, 42, 193664),
 datetime.datetime(2018, 9, 17, 15, 49, 42, 193664),
 datetime.datetime(2018, 9, 17, 16, 49, 42, 193664)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.