Python Get Current Year Example Tutorial

Published On: 25/03/2025 | Category: Python
How to Get the Current Year in Python?





Hi Developers,

Today, we’ll explore how to retrieve the current year in Python with simple and practical examples. Whether you're a beginner or an experienced Python enthusiast, this guide will show you how to extract the current year using Python's powerful datetime module. Let's dive into the details step by step!

How to Get the Current Year in Python?

In this post, I will provide three practical examples of obtaining the current year using Python's datetime module. Whether you need the full year or just the last two digits, these examples will have you covered.

Example 1: Fetch Current Year Using datetime.now()

 from datetime import datetime

today = datetime.now()

print("Current Date and Time:", today) print("Current Year:", today.year) 
Output:
 Current Date and Time : 2022-08-26 04:38:10.488591 Current Year : 2022 

Explanation:
Using datetime.now(), you can retrieve the current date and time. The year property helps us extract the full current year as a four-digit number.

Example 2: Get Current Year as Decimal Using strftime()

 from datetime import datetime

today = datetime.now()

year = today.strftime("%y") print("Current Year as decimal number:", year) 
Output:
 Current Year as decimal number : 22 

Explanation:
The strftime("%y") function formats the year as the last two digits (e.g., "22" for 2022). This is useful for applications requiring shortened year formats.

I hope this guide helps you implement current year extraction in your projects! Feel free to reach out if you have further questions or need additional examples. Happy coding!