• 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
  • Ruby
  • 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.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 2.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 3.  Set Widths of Columns in Word Document Table with python-docx
  • 4.  Python Split List into N Sublists
  • 5.  Random Number Without Repeating in Python
  • 6.  Multiply Each Element in List by Scalar Value with Python
  • 7.  Clear File Contents with Python
  • 8.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 9.  Using Python to Insert Tab in String
  • 10.  Run Something Every 5 Seconds 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy