5.4. Datetime Directives

  • Similar in almost all programming language

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

  • Source: [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 - UTC offset in the form +HHMM or -HHMM (empty string if the object is naive)

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

  • %a - Weekday as locale's abbreviated name

  • %A - Weekday as locale's full name

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

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

  • %B - Month as locale's full name

  • %b - Month as locale's abbreviated name

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

5.4.1. 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

5.4.2. 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

5.4.3. 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

5.4.4. 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

5.4.5. Minute

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

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

00
01
59

5.4.6. Seconds

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

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

00
01
59

5.4.7. 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

5.4.8. 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

5.4.9. 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

5.4.10. 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

5.4.11. Other

  • %% - A literal % character

%% - A literal % character:

%

5.4.12. References