Skip to main content
6 Ways to Get the Current Year in Python

6 Ways to Get the Current Year in Python

In this quick tutorial, I’ll show you six different approaches to getting the current year in Python. Python is a method and module-rich programming language that provides multiple ways to retrieve the current year.

How to Get the Current Year in Python

Let’s discuss different techniques in Python to get the current year. We want to access the current date and perform some operations on it to obtain results based on the current year.

  • Using the DateTime Module
  • Using strftime
  • By using now() method
  • Using the arrow External Library
  • Using Date String
  • Extract Year from Epoch Time

Using the Datetime Module

The datetime is an in-build Python module to deal with date and time. We can leverage the DateTime class and get the current date using today() method, subsequently, the current year.

from datetime import datetime

current_year = datetime.today().year
print("Current Year:", current_year)

The output will be:

Current Year: 2024

Using the strftime

The strftime method is the part of the datetime module, that allows developers to customize the formatting of date and time components. The function strftime() takes a string specifying the date format as the argument. It returns the given date as a string in the provided format.

We will pass "% Y" to strftime() to get the year of the date object.

from datetime import datetime

formatted_year = datetime.today().strftime('%Y')
print("Current Year:", formatted_year)

The output will be:

Current Year: 2024

By Using now() method

We can also use the now() method which are inside the datetime class under the datetime module. You can either access the year attribute or use the strftime() method to get the current year.

import datetime

d1 = datetime.datetime.now()
print("Current Year:", d1.year)
#using the strftime() method
d2 = d1.strftime("%Y")
print("Current Year:", year2)

The output will be:

Current Year: 2024
Current Year: 2024

Using the arrow External Library

We can also take leverages of external Python libraries, which provide additional options for retrieving the current year. You can include arrow, which offers enhanced functionalities for working with dates and times.

import arrow

current_year = arrow.now().year
print("Current Year:", current_year)

The output:

Current Year: 2024

Using Date String

Sometimes, We need to extract the current year from the date string. The strptime method from the datetime module allows you to parse a date string into a datetime object, from which you can then extract the year.

from datetime import datetime

#Example date string
date_string = "2024-01-15"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d")

#Extracting the year from the parsed date
current_year = parsed_date.year

print("Current Year:", current_year)

In this example, datetime.strptime is used to parse the date string “2024-01-15” with the specified format "%Y-%m-%d". The year component is then extracted using parsed_date.year from the resultant datetime object.

The output:

Current Year: 2024

Extract Year from Epoch Time

We can also extract the year from the EPOCH time (Unix timestamp) string. You can the datetime module along with the fromtimestamp method to convert the epoch time to a datetime object and then extract the year.

from datetime import datetime

@Unix timestamp
epoch_time =1705227668 # Replace this with your epoch time

#Converting epoch time to a datetime object
datetime_object = datetime.fromtimestamp(epoch_time)

#Extracting the year from the datetime object
current_year = datetime_object.year

print("Current Year", current_year)

In the above example, We convert the epoch time 1641568800 to a datetime object, and then use datetime_object.year method to extract the year from that object.

The output:

Current Year: 2024

Conclusion

Python has a vast collection of built-in libraries that empower developers to perform tedious tasks easily. This article explored five approaches, including built-in methods and third-party libraries, to retrieve the current year.

Leave a Reply

Your email address will not be published. Required fields are marked *