How to Get Today's Year in Python W3Schools Tutorial

Logan Jul 13, 2026

Getting the current year in Python is a straightforward task. Python's built-in datetime library provides a simple way to fetch the current year. However, if you're looking for a more concise way, Python provides a built-in attribute 'year' for this purpose. Let's delve into both methods and also explore how W3Schools can help in understanding Python's datetime library.

30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast

Before we start, make sure you have Python installed on your system. If not, you can download it from the official website - python.org.

Learn Python in 10 days
Learn Python in 10 days

Using the built-in 'year' attribute

The 'year' attribute in Python's datetime module allows you to get the current year more concisely. The datetime library fetches the current date and time when the program runs.

How to Learn Python for Free
How to Learn Python for Free

Here's how you can use it:

```python from datetime import date current_year = date.today().year print(current_year) ```

Understanding the code

Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners

The 'date' function from the 'datetime' module is used to get the current date. The 'today' function fetches the current date and time. The 'year' attribute is then used to print the current year.

The 'print' function is used to output the value of the 'current_year' variable.

Output

9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)

When you run this code, it will print the current year. For example, if the current year is 2023, the output will be:

``` 2023 ```

Using the datetime library

The 'datetime' library in Python offers more flexibility for date and time manipulation. Let's explore how to get the current year using this library.

Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]

Here's a simple way to do it:

```python from datetime import datetime current_year = datetime.now().year print(current_year) ```

Understanding the code

Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Free Python Learning Path for Beginners in 2025
Free Python Learning Path for Beginners in 2025
Top 16 Python Projects For 2025
Top 16 Python Projects For 2025
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Best Free Websites to Learn Python for Beginners
Best Free Websites to Learn Python for Beginners
Python Tutorial
Python Tutorial
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
30-Day Python Learning Roadmap for Beginners | Step-by-Step Coding Plan 2026
30-Day Python Learning Roadmap for Beginners | Step-by-Step Coding Plan 2026
the top 50 python project ideas
the top 50 python project ideas
Python Course For Beginners - GeeksforGeeks
Python Course For Beginners - GeeksforGeeks
What I Built After Learning Python for 30 Days 🐍🚀
What I Built After Learning Python for 30 Days 🐍🚀
Learn Python the Right Way in 5 Steps
Learn Python the Right Way in 5 Steps
Python Projects Ideas For Beginners
Python Projects Ideas For Beginners
Python Conditional Statements & Loops
Python Conditional Statements & Loops
Top 5 Websites to Learn Python for Free
Top 5 Websites to Learn Python for Free
7 Cool Python Projects for Kids to Try!
7 Cool Python Projects for Kids to Try!
Python tips for beginners ✨✨
Python tips for beginners ✨✨
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀

In this example, the 'now' function gets the current datetime. The 'year' attribute is then used to print the current year.

Again, the 'print' function is used to output the value of the 'current_year' variable.

W3Schools: A comprehensive guide to Python's datetime library

W3Schools is an excellent online resource for learning and understanding programming languages, including Python's datetime library. Their Python datetime tutorial - Python DateTime - provides a comprehensive guide with examples and intuitive explanations.

If you're new to Python's datetime library, we highly recommend checking out W3Schools' Python datetime tutorial to get a solid understanding of how to handle dates and times.

Remember, the code is just a starting point. Python offers numerous ways to solve a problem. Always look for opportunities to learn, experiment, and improve your coding skills. Happy coding!