API · Python
Python datetime.datetime.hour Attribute
The datetime.datetime.hour attribute stores the hour value of a datetime object.
On this page
The datetime is a built-in library in Python that offers several high-level classes to represent quantities such as dates (datetime.date), date and time combined (datetime.datetime), and time zones (tzinfo).
This article will shortly discuss the datetime.datetime.hour Attribute in Python.
Syntax of Python datetime.datetime.hour Attribute
<datetime object>.hour
Parameters
Since it is an attribute of a class, it does not have any parameters.
Returns
Since it is an attribute of a class, it does not return anything. It stores an hour value between 0 and 23, both inclusive.
Example Codes: Use the datetime.datetime.hour Attribute
import datetime
dt = datetime.datetime.now()
print("DateTime:", dt)
print("Hour:", dt.hour)
Output:
DateTime: 2022-08-24 14:51:23.727435
Hour: 14
The code example above creates a datetime object using the now() method and then prints the datetime object and the value of the hour attribute.