Print today's year, month, and day in Python

In this tutorial, we will learn how to get and print today's year, month, and day using Python program? By IncludeHelp Last updated : April 21, 2023

In the below example – we are implementing a python program to print the current/ today's year, month and year.

Steps to print today's year, month, and day

  • Import the date class from datetime module.
    from datetime import date
  • Create a date object of today's date and call the today() function of date class to get the current date.
    current_date = date.today()
  • By using the date object, extract and print today's year, month and day.
    current_date.year
    current_date.month
    current_date.day

Python program to print today's year, month, and day

# Python program to
# print today's year, month and day

# importing the date class datetime module
from datetime import date

# creating the date object of today's date
current_date = date.today() 

# printing the current date
print("Current date: ", current_date)

# extracting the current year, month and day
print("Current year:", current_date.year)
print("Current month:", current_date.month)
print("Current day:", current_date.day)

Output

Current date:  2020-03-09
Current year: 2020
Current month: 3
Current day: 9

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.