Show Current Day, Month, and Year in Python (3 Examples)

In this tutorial, I’ll illustrate how to return the current day, month, and year in the Python programming language.

Creation of Example Data

To return these specific attributes, we first have to load date from the datetime module:

from datetime import date          # Import date from datetime

We use the below date for the following examples:

date_x = date.today()              # Creating example date
print(date_x)
# 2021-06-04

Example 1: Return Day of Today

To extract the current day of our example date, we can use the attribute day:

actual_day = date_x.day            # Using attribute day
print(actual_day)
# 4

Example 2: Return Month of Today

For returning the current month of a date, we can apply the month attribute:

actual_month = date_x.month        # Using attribute month
print(actual_month) 
# 6

Example 3: Return Year of Today

Similar to the examples above, we can also use the attribute year for printing the current year:

actual_year = date_x.year          # Using attribute year
print(actual_year) 
# 2021

 

Matthias Bäuerlen Python Programmer

Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top