Date and time format options in Python

Python Methods and Functions

Modifiers (codes) to describe the date and time format.

The following format codes / directives are listed both from the 1989 C standard (work on all platforms that implement the standard) and additional (according to ISO 8601). 

Caution

ISO 8601 codes using strftime () may not be available on some platforms. ISO 8601 year and week directives are not compatible with those of 1989 C (cf. % G and % Y ). 


Note

Python uses the strftime () system function, so the full list of supported directives may vary for different platforms. This page lists the main ones. For a complete list, see the documentation for strftime ( debian ).


%% Used to get the % character %
Date and time
%c Date and time (by current locale) en_US: Thu 23 Jan 2020 20:40 : 33 AM +07 ; ru_RU: Thu 23 Jan 2020 20:40:33 1
% x Date (by current locale) en_US: 01/23/2020 ; ru_RU: 01/23/20201
%X Time (according to the current locale) en_US: 08:40:33 AM ; ru_RU: 20:40:331
Day
% d Month number (leading zero) 01, 02, 31 9
% j Year number (leading zero) 001, 002, 266 9
Week
% U Year number, two digits. First day of week: Sunday (previous days of the year are on week 0) 00, 01, 53 7, 9
% W Number in the year, two digits. First day of week: Monday (previous days of the year fall on week 0) 00, 01, 53 7, 9
% V + py3.6 ISO 8601. Year number, two digits. First day of the week: Monday; week 01 contains January 4th 01, 02, 53 8, 9
% a Short name of the day (according to the current locale) en_US: Sun, Mon; ru_RU: Sun, Mon 1
% А Full name of the day (according to the current locale) en_US: Thursday; ru_RU: Thursday 1
% w Day number, one digit (0 - Sunday, 6 - Saturday) 0, 1, 6
% u + py3.6 ISO 8601. Day number, one digit (1 - Monday) 1, 2, 7
Month
% b Short name (by current locale) en_US: Jan, Feb; ru_RU: Jan, Feb 1
% B Full name (according to the current locale) en_US: January; ru_RU: January 1
% m Number, two digits (leading zero) 01 , 02, 12 9
Year
% y Number, two digits (leading zero) 01, 02, 99 9
%Y Number, four digits (leading zero) 0001, 0002, 9999 2
% G + py3.6 ISO 8601. Number, four digits. The century includes most of the week (see %V) 0001, 2017, 9999 8
Time
% H Hour number, 24 hours, two digits (leading zero) 01, 02, 23 9
% M Minute, two digits (leading zero) 00, 01, 59 9
% S Second number, two digits (leading zero) 00, 01, 59 4, 9
% f + py2.6 Microsecond number, six digits (leading zero) 000000, 000001, 999999 5
% z Offset from UTC in the format . 063415, -030712.345216 6
% Z Timezone name & lt; empty & gt ;, UTC, EST, CST
% I Hour number, 12 o'clock, two digits (leading zero) 01, 02, 12 9
% p AM / PM half day indicator (by current locale ) e n_US: AM; ru_RU: '' 1, 3


1. Since the format depends on the current locale, it can change. The order of the fields may change. The output may include characters in the current locale-specific encoding.

2.  strptime () can parse years in the range [1, 9999] (since + py3.3; in + py3.2 - [1000, 9999]; formerly [1900, 9999]), however years & lt; 1000 must have four digits with leading zeros.

3. When using strptime () , the % p directive affects the output of the hour field if % I was used to parse the hour.

4 ... In contrast to the time module, the datetime module does not support leaping seconds.

5. When using strptime () , the % f directive supports one to six digits and zero padding to the right. % f is an extension of the C formatting character set implemented for datetime objects and therefore always available.

6. For naive objects, the value is always & lt; empty & gt ;. 

7. When using strptime () , the directives % U and % W are used in calculations only when specifying the day of the week and calendar year (% Y ).

8. When using strptime () , the % V directive, like % U and % W , is used in calculations only when specifying the day of the week and ISO year (% G ).

9. When using strptime () , for directives % d , % m , % H , % I , % M , % S , % J , % U , % W , % V leading zeros are optional. For the % y format, a leading zero is required .





Get Solution for free from DataCamp guru