Open In App

Get current date using Python

Last Updated : 02 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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: 

# 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:

# 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

# 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\

Get current date using Python -FAQs

How to Get the Current Date in Python?

To get the current date in Python, you can use the datetime module, specifically the date.today() method.

from datetime import date

# Get the current date
current_date = date.today()
print(current_date) # Output: YYYY-MM-DD (e.g., 2024-08-02)

How to Get the Current Date in Python in yyyy-mm-dd Format?

To format the current date as yyyy-mm-dd, you can use the strftime() method on a datetime object.

from datetime import datetime

# Get the current date and format it
current_date_formatted = datetime.now().strftime("%Y-%m-%d")
print(current_date_formatted) # Output: 2024-08-02

What is the Format of datetime.today() in Python?

The datetime.today() function from the datetime module returns the current local date and time with the default format including the time:

YYYY-MM-DD HH:MM:SS.mmmmmm
from datetime import datetime

# Get the current date and time
today_datetime = datetime.today()
print(today_datetime) # Output: 2024-08-02 12:34:56.789012

How Do I Get a Specific Date in Python?

To create a specific date in Python, you can use the datetime.date() constructor by passing the year, month, and day as arguments.

from datetime import date

# Create a specific date
specific_date = date(2024, 12, 25)
print(specific_date) # Output: 2024-12-25

These methods allow you to handle dates in Python effectively, whether you’re retrieving the current date or creating a date object for a specific day.


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg