datetime.date()
The datetime.date()
method returns a date object in the year-month-day format. It’s useful when there is a need of only the calendar date without time information.
Syntax
datetime.date(YYYY, MM, DD)
All parameters passed to the datetime.date()
method in the snippet above are required and must be passed in order. Otherwise, a TypeError
is thrown.
Parameters:
year
: A four-digit integer representing the year (e.g., 2025).month
: An integer representing the month (1 for January, 12 for December).day
: An integer representing the day of the month (must be valid for the given month and year).
Return value:
This method returns a date
object representing the specified calendar date in the format YYYY-MM-DD.
Example 1: Create and Access Date Components
Here’s a detailed example of using .date()
to create a date object and access its components:
import datetimeexample_date = datetime.date(2024, 10, 15)year = example_date.yearmonth = example_date.monthday = example_date.dayprint("Complete Date:", example_date)print("Year:", year)print("Month:", month)print("Day:", day)
The above code produces the following output:
Complete Date: 2024-10-15Year: 2024Month: 10Day: 15
Example 2: Get the Date of User Login
In this example, the date()
method is used to record a project deadline without including time details:
import datetimelogin_time = datetime.datetime(2024, 4, 9, 18, 30)login_date = login_time.date()print("User login date:", login_date)
The code produces the following output:
User login date: 2024-04-09
Codebyte Example 1: Check Expiry Date of a Product
This code compares the current date with a product’s expiry date, ignoring the time:
It’s perfect for real-world scenarios like food, coupons, or subscription expiration.
Note: The outputs produced for all these example codes will vary depending on the input dates and the current date when the code is run.
Frequenty Asked Questions
1. Can I use `.date()` on a `date` object?
No, the `.date()` method is only available on `datetime` objects. A date object already holds only `date` information.
2. What is the return type of `.date()`?
It returns a `datetime.date` object that contains the year, month, and day—but no time information.
3. Does `.date()` affect the original datetime?
No, `.date()` is non-destructive. It returns a new `date` object and does not modify the original `datetime` object.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours