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 |
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 |
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 |
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 |
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 |
actual_year = date_x.year # Using attribute year print(actual_year) # 2021
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.