140 of 151 menu

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 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
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline