6.4. Datetime Directives

  • Similar in almost all programming language

  • Some minor differences like in JavaScript minutes are i, not M

  • Source: [1]

Table 6.3. Frequently used date parsing directives. Examples for dt = datetime(1969, 7, 21, 2, 56, 15, tzinfo=timezone.utc)

Usage

Result

Description

dt.strftime('%Y')

'1969'

Year with century as a decimal number

dt.strftime('%m')

'07'

Month as a zero-padded decimal number

dt.strftime('%d')

'21'

Day of the month as a zero-padded decimal number

dt.strftime('%H')

'02'

Hour (24-hour clock) as a zero-padded decimal number

dt.strftime('%M')

'56'

Minute as a zero-padded decimal number

dt.strftime('%S')

'15'

Second as a zero-padded decimal number

dt.strftime('%f')

'000000'

Microsecond as a decimal number, zero-padded on the left

dt.strftime('%Z')

'UTC'

Time zone name (empty string if the object is naive)

dt.strftime('%z')

'+0000'

UTC offset in the form +HHMM or -HHMM (empty string if the object is naive)

dt.strftime('%A')

'Monday'

Weekday as locale's full name

dt.strftime('%a')

'Mon'

Weekday as locale's abbreviated name

dt.strftime('%B')

'July'

Month as locale's full name

dt.strftime('%b')

'Jul'

Month as locale's abbreviated name

dt.strftime('%I')

'02'

Hour (12-hour clock) as a zero-padded decimal number

dt.strftime('%p')

'AM'

Locale's equivalent of either AM or PM

6.4.1. Frequently Used

  • %Y - Year with century as a decimal number

  • %m - Month as a zero-padded decimal number

  • %d - Day of the month as a zero-padded decimal number

  • %H - Hour (24-hour clock) as a zero-padded decimal number

  • %M - Minute as a zero-padded decimal number

  • %S - Second as a zero-padded decimal number

  • %f - Microsecond as a decimal number, zero-padded on the left

  • %Z - Time zone name (empty string if the object is naive)

  • %z - UTC offset in the form +HHMM or -HHMM (empty string if the object is naive)

  • %A - Weekday as locale's full name

  • %a - Weekday as locale's abbreviated name

  • %B - Month as locale's full name

  • %b - Month as locale's abbreviated name

  • %I - Hour (12-hour clock) as a zero-padded decimal number

  • %p - Locale's equivalent of either AM or PM

Examples:

%Y-%m-%d                    # 1969-07-21
%H:%M:%S                    # 02:56:15
%Y-%m-%d %H:%M:%S.%f %Z     # 1969-07-21 02:56:15.123456 UTC
%A, %B %d, %Y at %I:%M %p   # Monday, July 21, 1969 at 02:56 AM

6.4.2. Year

  • %Y - Year with century as a decimal number

  • %y - Year without century as a zero-padded decimal number

  • %G - ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V)

%Y - Year with century as a decimal number

0001
0002
2024
2025
9998
9999

%y examples:

00
01
24
25
98
99

%G examples:

0001
0002
2024
2025
9998
9999

6.4.3. Month

  • %m - Month as a zero-padded decimal number

  • %B - Month as locale's full name

  • %b - Month as locale's abbreviated name

%m - Month as a zero-padded decimal number:

01
02
11
12

%B - Month as locale's full name:

January
February
November
December

%b - Month as locale's abbreviated name:

Jan
Feb
Nov
Dec

6.4.4. Day

  • %d - Day of the month as a zero-padded decimal number

  • %a - Weekday as locale's abbreviated name

  • %A - Weekday as locale's full name

  • %u - ISO 8601 weekday as a decimal number where 1 is Monday

  • %w - Weekday as a decimal number, where 0 is Sunday and 6 is Saturday

  • %j - Day of the year as a zero-padded decimal number

%d - Day of the month as a zero-padded decimal number:

01
02
30
31

%a - Weekday as locale's abbreviated name:

Mon
Tue
Wed
Thu
Fri
Sat
Sun

%A - Weekday as locale's full name:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

%u - ISO 8601 weekday as a decimal number where 1 is Monday:

1
2
3
4
5
6
7

%w - Weekday as a decimal number, where 0 is Sunday and 6 is Saturday:

0
1
2
3
4
5
6

%j - Day of the year as a zero-padded decimal number:

001
002
364
365

6.4.5. Hour

  • %H - Hour (24-hour clock) as a zero-padded decimal number

  • %I - Hour (12-hour clock) as a zero-padded decimal number

  • %p - Locale's equivalent of either AM or PM

%H - Hour (24-hour clock) as a zero-padded decimal number:

00
01
23

%I - Hour (12-hour clock) as a zero-padded decimal number:

01
02
12

%p - Locale's equivalent of either AM or PM:

AM
PM

6.4.6. Minute

  • %M - Minute as a zero-padded decimal number

%M - Minute as a zero-padded decimal number:

00
01
59

6.4.7. Seconds

  • %S - Second as a zero-padded decimal number

%S - Second as a zero-padded decimal number:

00
01
59

6.4.8. Micro Seconds

  • %f - Microsecond as a decimal number, zero-padded on the left

%f - Microsecond as a decimal number, zero-padded on the left:

000000
000001
999999

6.4.9. Timezone

  • %Z - Time zone name (empty string if the object is naive)

  • %z - UTC offset in the form +HHMM or -HHMM (empty string if the object is naive)

  • %:z - UTC offset in the form ±HH:MM[:SS[.ffffff]] (empty string if the object is naive).

%Z - Time zone name (empty string if the object is naive):

UTC
EST
CST

%z - UTC offset in the form +HHMM or -HHMM (empty string if the object is naive):

+0000
-0400
+1030

%:z - UTC offset in the form ±HH:MM[:SS[.ffffff]] (empty string if the object is naive):

+00:00
-04:00
+10:30

6.4.10. Week

  • %U - Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0

  • %W - Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0

  • %V - ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4

%U - Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0:

00
01
52
53

%W - Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0:

00
01
52
53

%V - ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4:

01
02
52
53

6.4.11. Presets

  • %c - Locale's appropriate date and time representation

  • %x - Locale's appropriate date representation

  • %X - Locale's appropriate time representation

%c - Locale's appropriate date and time representation:

Tue Aug 16 21:30:00 1988 (en_US)
Di 16 Aug 21:30:00 1988 (de_DE)

%x - Locale's appropriate date representation:

08/16/1988 (en_US)
16.08.1988 (de_DE)

%X - Locale's appropriate time representation:

21:30:00

6.4.12. Other

  • %% - A literal % character

%% - A literal % character:

%

6.4.13. References