HOWTO · Python

How to Get the Current Year in Python

Learn how to get the current year in Python with the standard-library datetime module.

On this page

Use Python’s standard-library datetime module to get the current year. Call datetime.datetime.now() and format its date with strftime("%Y") when you need a year string, or call datetime.date.today() and read its year attribute when you need an integer. Both approaches use the date available from the machine running the program, so the returned year changes when that machine’s date crosses into a new year.

Get the current year with strftime()

The strftime() method formats a date or time as a string. The %Y directive formats the year as a four-digit string, which is useful for display or for combining the year with other formatted date parts. The following example gets the current date and prints its year:

Run this example:

import datetime

current_date = datetime.datetime.now().date()
current_year = current_date.strftime("%Y")
print(f"Current year: {current_year}")
Current year: 2026

Here, datetime.datetime.now() supplies the current local date and time, and .date() keeps the date portion. Calling strftime("%Y") on that date returns a string, not a number. For example, if the runtime’s current year is 2026, current_year is the string "2026". The displayed year depends on the runtime clock; do not treat a sample value as a value that will remain current in later years.

You can use additional format directives to include other date details. For instance, %A %Y formats a weekday name followed by the year. The exact weekday depends on the date being formatted. See the Python documentation for strftime() and strptime() format codes for the complete list of directives and their platform notes.

Get the current year with the year attribute

When a numeric year is more convenient, get a datetime.date object and read its year attribute. datetime.date.today() returns today’s date directly, so this approach avoids creating a datetime.datetime value and converting it to a date first.

Run this example:

import datetime

current_date = datetime.date.today()
current_year = current_date.year
print(f"Current year: {current_year}")
Current year: 2026

The value of current_year here is an integer, unlike the string returned by strftime("%Y"). Use the integer for arithmetic or comparisons, and convert it to text when formatting it for display or combining it with other strings. Both datetime.datetime.now().date() and datetime.date.today() produce date objects whose year attribute can be read as date.year; choose the expression that best fits the date value you already have. For example, if your code already holds a date, read that object’s attribute rather than fetching today’s date again. The attribute reports the year on that date object, which may be different from the current year if the object represents a past or future date.

These examples use the machine’s local date. If your application needs the year in a particular timezone, start with an aware datetime, convert it to that timezone with astimezone(), and then read or format the converted datetime’s year. The conversion matters near New Year’s: the same instant can fall on different calendar dates, and therefore in different years, in different timezones. A plain date has no timezone information, so choose the timezone before deriving the date from an instant.