145 of 151 menu

datetime module strftime method

The strftime method of the datetime module returns a formatted string with date and time. In the method parameter, we specify the format we need.

Syntax

import datetime datetime.strftime(time format)

Syntax

The table shows the main time formats:

Format Description Example
%a Abbreviated regional names of days of the week. Sun, Mon, ..., Sat (en_US)
%A Full regional names of days of the week. Sunday, Monday, ..., Saturday (en_US)
%w Days of the week as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, ..., 6
%d Day of the month as a decimal number padded with zeros. 01, 02, ..., 31
%b Abbreviated regional name of the month. Jan, Feb, ..., Dec (en_US)
%B Full regional name of the month. January, February, ..., December (en_US)
%m Month as a decimal number padded with zeros. 01, 02, ..., 12
%y The year without specifying the century as a decimal number padded with zeros. 00, 01, ..., 99
%Y Year with century indicated as a decimal number padded with zeros. 0001, ..., 2013, 2014

Example

Let's create a date and time object that specifies the year and century, the month name in decimal format, and the day of the month as a decimal number:

import datetime dt = datetime.datetime(2025, 12, 31, 12, 59, 59) res = dt.strftime('%Y-%m-%d') print(res)

The result of the executed code:

2025-12-31

See also

  • method time of module time,
    which returns the time in seconds since the epoch
  • method ctime of module time,
    which converts seconds to a datetime string
  • method mktime of module time,
    which converts a date and time string to 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