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
import datetime
datetime.date(year, month, day)
Example
Let's create a new date object:
import datetime
res = datetime.date(2021, 12, 31)
print(res)
The result of the executed code:
'2021-12-31'
Example
Now let's derive the day, month and year separately from our date object:
import datetime
res = datetime.date(2021, 12, 31)
print(res.day)
print(res.month)
print(res.year)
The result of the executed code:
31
12
2021
See also
-
method
datetimeof moduledatetime,
which creates a date and time object -
method
timeof moduledatetime,
which creates a time object -
method
todayof moduledatetime,
which returns the current date -
method
isleapof the calendar module,
which determines a leap year -
method
sleepof moduletime,
which stops the operation from running for the specified number of seconds