Python datetime.date() Method

Vaibhav Vaibhav Jan 30, 2023
  1. Syntax of Python datetime.date() Method
  2. Example Code: Use the datetime.date() Method in Python
Python datetime.date() Method

The datetime module is a robust module available in Python. It deals with dates, times, and time zones and offers reliable utilities and solutions.

This module implements a class datetime that stores date along with time and timezone. This class has various instance methods, datetime.date being one that returns a date object for the exact date.

Syntax of Python datetime.date() Method

<datetime object>.date()

Parameters

This method does not accept any parameters.

Returns

This method returns a date object for the datetime object date. The date class is a part of the datetime module.

It accepts three arguments, namely, year, month, and day and it offers various methods to work with the object. To learn about the datetime.date class, refer to the official Python documentation here.

Example Code: Use the datetime.date() Method in Python

from datetime import datetime

dt = datetime.now()
print("DateTime object:", dt)
print(type(dt))
date = dt.date()
print("Date object:", date)
print(type(date))

Output:

DateTime object: 2022-08-23 12:22:02.813862
<class 'datetime.datetime'>
Date object: 2022-08-23
<class 'datetime.date'>

The Python code above first creates a datetime object using the now() method that, as the name suggests, returns a datetime instance for the current moment. Next, we extract a date object from it with the help of the date() method.

The outputs prove that the former is a datetime object and the latter is a date object, which refers to the exact moment or day.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python Datetime