The date method of datetime module

The date method of the datetime module creates a date object. In the first parameter of the method we specify the year, in the second parameter - the month, in the third - the day.

Syntax

pythonimport datetime

datetime.date(year, month, day)

Example

Let's create a new date object:

pythonimport datetime

res = datetime.date(2021, 12, 31)
print(res)

The result of the executed code:

python'2021-12-31'

Example

Now let's derive the day, month and year separately from our date object:

pythonimport datetime

res = datetime.date(2021, 12, 31)
print(res.day)
print(res.month)
print(res.year)

The result of the executed code:

python31
12
2021

See also

  • method datetime of module datetime,
    which creates a date and time object
  • method time of module datetime,
    which creates a time object
  • method today of module datetime,
    which returns the current date
  • method isleap of the calendar module,
    which determines a leap year
  • method sleep of module time,
    which stops the operation from running for the specified number of seconds