API · Python
Python datetime.datetime.month Attribute
The datetime.datetime.month attribute stores the month number 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 briefly discuss Python’s datetime.datetime.month Attribute.
Syntax of Python datetime.datetime.month Attribute
<datetime object>.month
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 the month number between 1 and 12.
Example Codes: Use datetime.datetime.monthAttribute in Python
import datetime
dt = datetime.datetime.now()
print("DateTime:", dt)
print("Month:", dt.month)
Output:
DateTime: 2022-08-24 14:39:44.782452
Month: 8
The Python code above first creates a datetime object using the now() method and then prints the datetime object and the value of the month attribute.