Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get hour of year from a Datetime

Is there a simple way to obtain the hour of the year from a datetime?

dt = datetime(2019, 1, 3, 00, 00, 00) # 03/01/2019 00:00
dt_hour = dt.hour_of_year() # should be something like that

Expected output: dt_hour = 48

It would be nice as well to obtain minutes_of_year and seconds_of_year

like image 806
ironzionlion Avatar asked Aug 25 '20 10:08

ironzionlion


People also ask

How do you find the hour from a datetime?

How to Get the Current Time with the datetime Module. To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.

How do I convert datetime to hours in Python?

to check if the difference is more than 6 hours you can just do abs(start_time - end_time) > timedelta(hours=6) .

How do I get hours from Timedelta?

How to convert a timedelta to hours. We can follow the same logic to convert a timedelta to hours. Instead of dividing the total_seconds() by the number of seconds in a minute, or dividing the timedelta object by timedelta(minutes=1) , we do it for hour.


3 Answers

One way of implementing this yourself is this:

def hour_of_year(dt): 
    beginning_of_year = datetime.datetime(dt.year, 1, 1, tzinfo=dt.tzinfo)
    return (dt - beginning_of_year).total_seconds() // 3600

This first creates a new datetime object representing the beginning of the year. We then compute the time since the beginning of the year in seconds, divide by 3600 and take the integer part to get the full hours that have passed since the beginning of the year.

Note that using the days attribute of the timedelta object will only return the number of full days since the beginning of the year.

like image 182
Sven Marnach Avatar answered Oct 09 '22 16:10

Sven Marnach


You can use timedelta:

import datetime
dt = datetime.datetime(2019, 1, 3, 00, 00, 00)
dt2 = datetime.datetime(2019, 1, 1, 00, 00, 00)
print((dt-dt2).days*24)

output:

48
like image 2
Mehdi Mostafavi Avatar answered Oct 09 '22 15:10

Mehdi Mostafavi


All three functions, reusing their code.

import datetime

def minutes_of_year(dt):
    return seconds_of_year(dt) // 60

def hours_of_year(dt):
    return minutes_of_year(dt) // 60

def seconds_of_year(dt):
    dt0 = datetime.datetime(dt.year, 1, 1, tzinfo=dt.tzinfo)
    delta = dt-dt0
    return int(delta.total_seconds())

Edited to take possible time zone info into account.

Or: subclass datetime, for easier reuse in later projects:

import datetime

class MyDateTime(datetime.datetime):
    def __new__(cls, *args, **kwargs):
        return datetime.datetime.__new__(cls, *args, **kwargs)

    def minutes_of_year(self):
        return self.seconds_of_year() // 60

    def hours_of_year(self):
        return self.minutes_of_year() // 60

    def seconds_of_year(self):
        dt0 = datetime.datetime(self.year, 1, 1, tzinfo=self.tzinfo)
        delta = self-dt0
        return int(delta.total_seconds())

# create and use like a normal datetime object
dt = MyDateTime.now()
# properties and functions of datetime still available, of course.
print(dt.day)
# ... and new methods:
print(dt.hours_of_year())
like image 1
Wups Avatar answered Oct 09 '22 15:10

Wups