Python datetime.datetime.year Attribute

Python datetime.datetime.year Attribute

  1. Syntax of Python datetime.datetime.year Attribute
  2. Examples Codes: Use datetime.year() in Python

The datetime module is a built-in module available in Python programming language. This module is a warehouse of robust and optimized functions and classes to work with dates, dates and times, time deltas, and time zones.

It offers various high-level classes to represent quantities such as dates (datetime.date), time (datetime.time), time deltas (datetime.timedelta), date and time together (datetime.datetime), and timezones (datetime.timezone).

Every datetime.datetime class instance has access to various attributes to get atomic values of the represented date and time, such as month, hour, day, year, etc. In this article, we will talk about the datetime.datetime.year attribute.

Syntax of Python datetime.datetime.year Attribute

<datetime object>.year

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 year of a datetime.datetime object.

Examples Codes: Use datetime.year() in Python

import datetime

dt = datetime.datetime.now()
print("DateTime:", dt)
print("Year:", dt.year)

Output:

DateTime: 2022-09-08 05:29:39.971356
Year: 2022

We create a datetime object using the now() method and then print the datetime object and the value of the year 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.

LinkedIn GitHub

Related Article - Python DateTime

  • Python datetime.tzinfo Class
  • Python datetime.datetime.replace() Method
  • Python datetime.timezone Class
  • Python Datetime.fromtimestamp() Method
  • Python Datetime.timedelta() Method