• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Get Current Year in Python

Get Current Year in Python

March 5, 2022 Leave a Comment

When using Python, there are a number of ways to get the current year in Python. The easiest way to get the current year is to use the datetime.today() function from the datetime module and access the “year” attribute.

import datetime 

currentDate = datetime.date.today()

print(currentDate.year)

#Output:
2022

You can also use the strftime function to get the current year in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y"))

#Output:
2022

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to dates, sometimes we need to display the current date.

With Python, we can easily get the current year.

The Python datetime module has many great functions which allow us to interact with date and datetime objects easily.

The easiest way to get the current year is to use the datetime.today() function from the datetime module and access the “year” attribute.

import datetime 

currentDate = datetime.date.today()

print(currentDate.year)

#Output:
2022

Another way to get the current year in Python is with the strfttime() function. strftime() accepts a string representing the format of a date and returns the date as a string in the given format.

To get the year of the date object with strftime(), pass “%Y”.

Below is an example of how to use strftime function to get the current year in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y"))

#Output:
2022

Get Current Day in Python

If you are looking to get the current day in Python, you can modify the code the from above. All date objects in Python have the following attributes: ‘day’, ‘month’, ‘weekday’, and ‘year’.

To get the current day from a date object, instead of accessing the ‘year’ attribute, we access the ‘day’ attribute.

Below is an example of how to get the current day using Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.day)

#Output:
5

Like above, we can also use strftime() to get the current day. Instead of passing “%Y”, pass “%d”.

Below is an example of how to use strftime function to get the current day in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%d"))

#Output:
05

Get Current Month in Python

If you are looking to get the current day in Python, you can modify the code the from above. As mentioned above, all date objects in Python have the following attributes: ‘day’, ‘month’, ‘weekday’, and ‘year’.

To get the current month from a date object, instead of accessing the ‘year’ attribute, we access the ‘month’ attribute.

Below is an example of how to get the current month using Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.month)

#Output:
3

Like above, we can also use strftime() to get the current day. Instead of passing “%Y”, pass “%m”.

Below is an example of how to use strftime function to get the current month in Python.

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%m"))

#Output:
03

Hopefully this article has been useful for you to learn how to get the current year in Python using the datetime module.

Other Articles You'll Also Like:

  • 1.  Using Python to Replace Backslash in String
  • 2.  Loop Through Files in Directory Using Python
  • 3.  How to Remove All Occurrences of a Character in a List Using Python
  • 4.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 5.  Remove Last Element from List Using Python
  • 6.  Append Multiple Elements to List Using Python
  • 7.  Python sin – Find Sine of Number in Radians Using math.sin()
  • 8.  Adjusting Python Turtle Screen Size with screensize() Function
  • 9.  How to Check if String Contains Lowercase Letters in Python
  • 10.  Swap Two Values of Variables in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy