Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Get current year in Python

Get current year in Python

Author image

Harsh Pandey

Software Developer

Published on Thu Mar 28 2024

Introduction

Welcome to our blog on "Python get current year"! If you're new to Python programming or just starting to explore its functionalities, you might have wondered how to fetch the current year in your code. In this blog, we'll walk you through two simple methods using the "today()" method and the "now()" method, and you'll be able to effortlessly obtain the current year in Python.

Whether you're working on a personal project or a professional application, knowing the current year can be essential for various date-based operations. Let's dive in and learn these methods step-by-step!

Python get current year using today() method

Accessing the year attribute

To get the current year using the "today()" method, we first need to import the "date" class from the "datetime" module. This class allows us to work with dates easily.

from datetime import date

# Get the current date
current_date = date.today()

# Access the year attribute to get the current year
current_year = current_date.year

# Print the result
print("Current Year:", current_year)

In this code snippet, we import the "date" class from the "datetime" module and then use the "today()" method to get the current date, which includes the year information. We then access the "year" attribute of the current date to extract the year value. Finally, we print the result, which will display the current year.

Using strftime() function

The "strftime()" function allows us to format dates in Python. We can use it to extract the year from the current date as well. Here's how you can do it:

from datetime import date

# Get the current date
current_date = date.today()

# Use strftime() to format the date and extract the year
current_year = current_date.strftime("%Y")

# Print the result
print("Current Year:", current_year)

In this example, we import the "date" class and use the "today()" method to get the current date. Then, we apply the "strftime()" function to format the date. The format code "%Y" represents the year in a four-digit format. Using this format code, we extract the year from the current date and store it in the "current_year" variable. Finally, we print the current year.

Python get current year using now() method

The "now()" method from the "datetime" module provides us with both date and time information. To get only the current year, we can follow the steps below:

from datetime import datetime

# Get the current date and time
current_datetime = datetime.now()

# Access the year attribute to get the current year
current_year = current_datetime.year

# Print the result
print("Current Year:", current_year)

In this code, we import the "datetime" class and use the "now()" method to get the current date and time. We then access the "year" attribute of the "current_datetime" variable to extract the current year. The result will display the current year.

Conclusion

In this blog post, we explored two methods to get the current year in Python. Using the "today()" method from the "datetime" module, we could access the "year" attribute of the date object to directly obtain the current year. Alternatively, we used the "now()" method to get the current date and time in a "datetime" object and then accessed the "year" attribute to extract the year.

Both methods are straightforward and effective for retrieving the current year in Python. Depending on your specific use case, you can choose the method that best fits your needs.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.