Open In App

Get current date using Python

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: DateTime module

In Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some functions to get the current date as well as time.

Get the current date using date.today()

The today() method of date class under DateTime module returns a date object which contains the value of Today’s date.

Syntax: date.today() 

Returns: Return the current local date.

Example: 

Python3




# Import date class from datetime module
from datetime import date
 
# Returns the current local date
today = date.today()
print("Today date is: ", today)


Output:

Today date is:  2019-12-11

Get the current date using datetime.now()

Python library defines a function that can be primarily used to get the current time and datetime.now() function and return the current local date and time, which is defined under the DateTime module.

Syntax: datetime.now(tz) 

Parameters : tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.) Returns : Returns the current date and time in time format.

Example:

Python3




# Import datetime class from datetime module
from datetime import datetime
 
# returns current date and time
now = datetime.now()
print("now = ", now)


Output:

now =  2019-12-11 10:58:37.039404

Attributes of now()

The now() has different attributes, same as attributes of time such as year, month, date, hour, minute, and second. 

Example

Python3




# importing datetime module for now()
import datetime
     
# using now() to get current time
current_time = datetime.datetime.now()
     
# Printing attributes of now().
print ("The attributes of now() are : ")
     
print ("Year: ", end = "")
print (current_time.year)
     
print ("Month: ", end = "")
print (current_time.month)
     
print ("Day: ", end = "")
print (current_time.day)


Output:

The attributes of now() are: 
Year: 2019
Month: 12
Day: 11

RECOMMENDED ARTICLES – Get Current Date and Time using Python



Last Updated : 18 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads