Gustavo Reyes
Gustavo Reyes

Reputation: 1344

Calculate hour range between dates in python

How can I find out the hours in a date range? I'm currently calculating and printing the dates between two given dates.

from datetime import timedelta as td, datetime

d1 = datetime.strptime(start_date, '%Y-%m-%d')
d2 = datetime.strptime(end_date, '%Y-%m-%d')

def get_delta(d1, d2):
    delta = d2 - d1
    return delta

delta = get_delta(d1,d2)
for i in range(delta.days + 1):
    print d1 + td(days=i)

Current output:

2016-04-22
2016-04-23
2016-04-24
2016-04-25
2016-04-26
2016-04-27
2016-04-28
...

Expected output:

2016-04-22 00:00:00
2016-04-22 01:00:00
2016-04-22 02:00:00
2016-04-22 03:00:00
2016-04-22 04:00:00
2016-04-22 05:00:00
2016-04-22 06:00:00
2016-04-22 07:00:00
...

Upvotes: 3

Views: 10647

Answers (5)

Claudia
Claudia

Reputation: 1016

Using pandas

import pandas as pd
datelist = pd.date_range(start = pd.datetime(2016, 1, 1, 0),
                         end = pd.datetime(2016, 1, 2, 0),
                         freq = "H")

Upvotes: 0

jfs
jfs

Reputation: 414875

For readability, to have a clear semantics of what happens in the edge cases, you could define date_range() function that is similar to the builtin range() function:

def date_range(start, end, step):
    while start < end:
        yield start
        start += step

Example:

from datetime import datetime, timedelta

HOUR = timedelta(hours=1)

def parse(date_string):
    '''Parse *date_string* in the %Y-%m-%d format.'''
    return datetime(*map(int, date_string.split('-')))

for dt in date_range(parse(start_date), parse(end_date), HOUR):
    print(dt)

Note: end_date + ' 00:00:00 is not present in the output. date_range() behaves like range() where end is not included. You could pass parse(end_date) + HOUR to include the end_date.

Upvotes: 2

Michael Robellard
Michael Robellard

Reputation: 2368

Try something like this:

import datetime

d1 = datetime.datetime(2016, 4, 28)
d2 = d1 + datetime.timedelta(days = 2)

delta = (d2 - d1).total_seconds()/3600
for i in range(int(delta)):
    print(d1 + datetime.timedelta(hours=i))

Upvotes: 1

Maximilian Peters
Maximilian Peters

Reputation: 31739

You are incrementing the time by days and not hours, timedelta(days=i) and not timedelta(hours=i)

from datetime import timedelta as td, datetime

start_date = '2016-01-01'
end_date = '2016-01-02'
d1 = datetime.strptime(start_date, '%Y-%m-%d')
d2 = datetime.strptime(end_date, '%Y-%m-%d')

def get_delta(d1, d2):
    delta = d2 - d1
    return delta

delta = get_delta(d1,d2)
for i in range(delta.days * 24 + 1):
    print d1 + td(hours=i)

Output:

2016-01-01 00:00:00
2016-01-01 01:00:00
...
...
2016-01-01 23:00:00
2016-01-02 00:00:00

Upvotes: 7

Michael
Michael

Reputation: 198

If you look at the python docs for timedelta objects:

Only days, seconds and microseconds are stored internally

So if you want to get the hours you'll have to convert it from the seconds.

Something like:

for i in range(delta.seconds / 3600 + delta.days * 24):
    print d1 + td(seconds=3600 * i)

Upvotes: 1

Related Questions