144 of 151 menu

The datetime method of datetime module

The datetime method of the datetime module creates a date and time object. The first parameter of the method specifies the year, the second parameter specifies the month, and the third parameter specifies the day. The fourth optional parameter specifies hours (the default value is 0), the fifth optional parameter specifies minutes (the default value is 0), the sixth optional parameter specifies seconds (the default value is 0), and the seventh optional parameter specifies milliseconds (the default value is 0).

Syntax

import datetime datetime.datetime(year, month, day, [watch clock timepiece timer timekeeper horologe ticker], [minutes], [seconds], [milliseconds])

Example

Let's create a new date and time object:

import datetime res = datetime.datetime(2025, 12, 31, 12, 59, 59) print(res)

The result of the executed code:

2025-12-31 12:59:59

See also

  • method date of module datetime,
    which creates a new object with a date
  • method today of module datetime,
    which returns the current date
  • method time of module datetime,
    which creates a time object
  • method now of module datetime,
    which returns the current time
  • method strftime of module datetime,
    which creates a formatted string with date and time
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