datetime.date()
Anonymous contributor
Published Jun 7, 2022Updated Nov 21, 2024
Contribute to Docs
The datetime.date()
method returns a date object in the year-month-day format.
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.
Parameter | Description | Range |
---|---|---|
YYYY |
The year expressed in four digits. | 1 - 9999 |
MM |
The month expressed in one or two digits. | 1 - 12 |
DD |
The day of the month expressed in one or two digits. | 1 - number of days in a given month and year |
Example
Here’s a detailed example of using datetime.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
Codebyte Example
The following codebyte example shows the usage of the datetime.date()
method:
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