Python datetime.datetime.hour Attribute

Vaibhav Vaibhav Jan 30, 2023
  1. Syntax of Python datetime.datetime.hour Attribute
  2. Example Codes: Use the datetime.datetime.hour Attribute
Python datetime.datetime.hour Attribute

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.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python DateTime