Date and time formatting directives

更新时间:
复制 MD 格式

Lists all supported date and time formatting directives with examples and usage notes.

All directives are fully compatible with the C standard (1989 version).

Directive

Description

Example

Notes

%a

Abbreviated weekday name.

Mon, ..., Fri

Only the en-US locale is currently supported.

%A

Full weekday name.

Monday, ..., Friday

Only the en-US locale is currently supported.

%w

Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.

0, 1, 2, 3, 4, 5, 6

None.

%d

Day of the month as a zero-padded decimal number.

01, 02, ..., 31

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%b

Abbreviated month name for the locale.

Jan, Feb, ..., Dec

Only the en-US locale is currently supported.

%B

Full month name for the locale.

January, February, ..., December

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%m

Month as a zero-padded decimal number.

01, 02, ..., 12

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%y

Year without century as a zero-padded decimal number.

00, 01, ..., 99

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%Y

Year with century as a decimal number.

0001, 0002, ..., 2013, 2014, ..., 9998, 9999

When parsing a year, the valid range is [1, 9999]. If a year is less than 1000, it must be padded with zeros to four digits. For example, 0180 represents the year 180 AD.

%H

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

00, 01, ..., 23

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%I

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

01, 02, ..., 12

When parsing, leading zeros are not required for %d, %m, %H, %I, %M, %S, %J, %U, %W, %V, %y.

%p

The locale's equivalent of AM or PM.

AM, PM

  • Only the en-US locale is currently supported.

  • When parsing, the %p directive affects only the hour part of %I.

%M

Minute as a zero-padded decimal number.

00, 01, ..., 59

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%S

Second as a zero-padded decimal number.

00, 01, ..., 59

  • Leap seconds are not supported.

  • When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%f

Microsecond as a zero-padded decimal number.

000000, 000001, ..., 999999

When parsing microseconds, the %f directive accepts a numeric string of 0 to 6 characters.

%z

UTC offset in the ±HHMM[SS[.ffffff]] format. Returns an empty string for a naive datetime object.

(empty), +0000, -0400, +1030, +063415, -030712.345216

For a naive datetime object, %z and %Z are replaced with an empty string. When %z is formatted as ±HHMM[SS[.ffffff]], minutes are not required. During parsing, colon-separated strings such as +01:00:00 are supported. Additionally, Z is equivalent to +00:00.

%Z

Time zone name. Returns an empty string if the object is naive.

(empty), UTC, EST, CST

None.

%j

Day of the year as a zero-padded decimal number.

001, 002, ..., 366

When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%U

Week number of the year, with Sunday as the first day of the week. Days before the first Sunday of the year are in week 0.

00, 01, ..., 53

  • When parsing, the %U and %W directives are used for calculation only.

  • When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%W

Week number of the year, with Monday as the first day of the week. Days before the first Monday of the year are in week 0.

00, 01, ..., 53

  • When parsing, the %U and %W directives are used for calculation only.

  • When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.

%c

The locale's appropriate date and time representation.

Tue Aug 16 21:30:00 1988

Only the en-US locale is currently supported.

%x

The locale's appropriate date representation.

08/16/88

Only the en-US locale is currently supported.

%X

The locale's appropriate time representation.

21:30:00

Only the en-US locale is currently supported.

%%

A literal '%' character.

%

None.

The following non-standard C89 directives are also supported:

Directive

Description

Example

Notes

%G

ISO 8601 week-based year. This is the year that contains the ISO week (%V).

0001, 0002, ..., 2013, 2014, ..., 9998, 9999

When parsing, %V is used for calculation only.

%u

ISO 8601 weekday as a decimal number, where 1 is Monday.

1, 2, ..., 7

None.

%V

ISO 8601 week number of the year (Monday as the first day of the week).

01, 02, ..., 53

  • When parsing, %V is used for calculation only.

  • When parsing, a leading zero is not required for the %d, %m, %H, %I, %M, %S, %j, %U, %W, %V, %y directives.