When using Python, there are a number of ways to get the current year in Python. The easiest way to get the current year is to use the datetime.today() function from the datetime module and access the “year” attribute.

import datetime 

currentDate = datetime.date.today()

print(currentDate.year)

#Output:
2022

You can also use the strftime function to get the current year in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y"))

#Output:
2022

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to dates, sometimes we need to display the current date.

With Python, we can easily get the current year.

The Python datetime module has many great functions which allow us to interact with date and datetime objects easily.

The easiest way to get the current year is to use the datetime.today() function from the datetime module and access the “year” attribute.

import datetime 

currentDate = datetime.date.today()

print(currentDate.year)

#Output:
2022

Another way to get the current year in Python is with the strfttime() function. strftime() accepts a string representing the format of a date and returns the date as a string in the given format.

To get the year of the date object with strftime(), pass “%Y”.

Below is an example of how to use strftime function to get the current year in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y"))

#Output:
2022

Get Current Day in Python

If you are looking to get the current day in Python, you can modify the code the from above. All date objects in Python have the following attributes: ‘day’, ‘month’, ‘weekday’, and ‘year’.

To get the current day from a date object, instead of accessing the ‘year’ attribute, we access the ‘day’ attribute.

Below is an example of how to get the current day using Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.day)

#Output:
5

Like above, we can also use strftime() to get the current day. Instead of passing “%Y”, pass “%d”.

Below is an example of how to use strftime function to get the current day in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%d"))

#Output:
05

Get Current Month in Python

If you are looking to get the current day in Python, you can modify the code the from above. As mentioned above, all date objects in Python have the following attributes: ‘day’, ‘month’, ‘weekday’, and ‘year’.

To get the current month from a date object, instead of accessing the ‘year’ attribute, we access the ‘month’ attribute.

Below is an example of how to get the current month using Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.month)

#Output:
3

Like above, we can also use strftime() to get the current day. Instead of passing “%Y”, pass “%m”.

Below is an example of how to use strftime function to get the current month in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%m"))

#Output:
03

Hopefully this article has been useful for you to learn how to get the current year in Python using the datetime module.

Categorized in:

Python,

Last Update: March 14, 2024