1import datetime
2
3today = datetime.datetime.now()
4date_time = today.strftime("%m/%d/%Y, %H:%M:%S")
5print("date and time:",date_time)
1
2from datetime import datetime
3
4now = datetime.now() # current date and time
5
6year = now.strftime("%Y")
7print("year:", year)
8
9month = now.strftime("%m")
10print("month:", month)
11
12day = now.strftime("%d")
13print("day:", day)
14
15time = now.strftime("%H:%M:%S")
16print("time:", time)
17
18date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
19print("date and time:",date_time)
20
21
22-------------------------------------------------------------------------
23Directive Meaning Example
24%a Abbreviated weekday name. Sun, Mon, ...
25%A Full weekday name. Sunday, Monday, ...
26%w Weekday as a decimal number. 0, 1, ..., 6
27%d Day of the month as a zero-padded decimal. 01, 02, ..., 31
28%-d Day of the month as a decimal number. 1, 2, ..., 30
29%b Abbreviated month name. Jan, Feb, ..., Dec
30%B Full month name. January, February, ...
31%m Month as a zero-padded decimal number. 01, 02, ..., 12
32%-m Month as a decimal number. 1, 2, ..., 12
33%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
34%-y Year without century as a decimal number. 0, 1, ..., 99
35%Y Year with century as a decimal number. 2013, 2019 etc.
36%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
37%-H Hour (24-hour clock) as a decimal number. 0, 1, ..., 23
38%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
39%-I Hour (12-hour clock) as a decimal number. 1, 2, ... 12
40%p Locale’s AM or PM. AM, PM
41%M Minute as a zero-padded decimal number. 00, 01, ..., 59
42%-M Minute as a decimal number. 0, 1, ..., 59
43%S Second as a zero-padded decimal number. 00, 01, ..., 59
44%-S Second as a decimal number. 0, 1, ..., 59
45%f Microsecond as a decimal number, zero-padded on the left. 000000 - 999999
46%z UTC offset in the form +HHMM or -HHMM.
47%Z Time zone name.
48%j Day of the year as a zero-padded decimal number. 001, 002, ..., 366
49%-j Day of the year as a decimal number. 1, 2, ..., 366
50%U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, ..., 53
51%W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, ..., 53
52%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
53%x Locale’s appropriate date representation. 09/30/13
54%X Locale’s appropriate time representation. 07:06:05
55%% A literal '%' character. %
56-------------------------------------------------------------------------
57
1# exemple
2print(date.strftime("%Y/%m/%d")) #2020/12/31
3# all format codes
4
5# %a | Weekday as locale’s abbreviated name. | Sun, Mon, …, Sat (en_US)
6# ---|--------------------------------------------------------------------
7# %A | Weekday as locale’s full name. | Sunday, Monday, …, Saturday
8# ------------------------------------------------------------------------
9# %w | Weekday as a decimal number, | 0, 1, ...,6
10# | where 0 is Sunday and 6 is Saturday. |
11# ------------------------------------------------------------------------
12# %d | Day of the month as a zero-padded | 01, 02, …, 31
13# | decimal number. |
14# ------------------------------------------------------------------------
15# %b | Month as locale’s abbreviated name. | Jan, Feb, …, Dec
16# ------------------------------------------------------------------------
17# %B | Month as locale’s full name. | January, February, …, December
18# ------------------------------------------------------------------------
19# %m | Month as a zero-padded decimal number.| 01, 02, …, 12
20# ------------------------------------------------------------------------
21# %y | Year without century as a zero-padded | 00, 01, …, 99
22# | decimal number. |
23# ------------------------------------------------------------------------
24# %Y | Year with century as a decimal number.| 0001, 0002, …, 9999
25# ------------------------------------------------------------------------
26# %H | Hour (24-hour clock) as a zero-padded | 00, 01, …, 23
27# | decimal number. |
28# ------------------------------------------------------------------------
29# %I | Hour (12-hour clock) as a zero-padded | 01, 02, …, 12
30# | decimal number. |
31# ------------------------------------------------------------------------
32# %p | Locale’s equivalent of either AM / PM | AM, PM (en_US);
33# ------------------------------------------------------------------------
34# %M | Minute as a zero-padded decimal number| 00, 01, …, 59
35# ------------------------------------------------------------------------
36# %S |Second as a zero-padded decimal number.| 00, 01, …, 59
37# ------------------------------------------------------------------------
38# %f | Microsecond as a decimal number, | 000000, 000001, …, 999999
39# | zero-padded on the left. |
40# ------------------------------------------------------------------------
41# %z | UTC offset in the form | (empty), +0000, -0400, +1030,
42# | ±HHMM[SS[.ffffff]] | +063415, -030712.345216
43# | (empty string if the object is naive) |
44# ------------------------------------------------------------------------
45# %Z | Time zone name | (empty), UTC, GMT
46# | (empty string if the object is naive) |
47# ------------------------------------------------------------------------
48# %j | Day of the year as a zero-padded | 001, 002, …, 366
49# | decimal number. |
50# ------------------------------------------------------------------------
51# %U | Week number of the year | 00, 01, …, 53
52# | (Sunday as the first day of the week) |
53# | as a zero padded decimal number. |
54# | All days in a new year preceding the |
55# | first Sunday are considered to be |
56# | in week 0. |
57# ------------------------------------------------------------------------
58# %W | Week number of the year | 00, 01, …, 53
59# | (Monday as the first day of the week) |
60# | as a decimal number. All days in a |
61# | new year preceding the first Monday |
62# | are considered to be in week 0. |
63# ------------------------------------------------------------------------
64# %c | Locale’s appropriate date and | Tue Aug 16 21:30:00 1988 (en_US)
65# | time representation. | Di 16 Aug 21:30:00 1988 (de_DE)
66# ------------------------------------------------------------------------
67# %x | Locale’s appropriate date | 08/16/88 (None)
68# | representation. | 08/16/1988 (en_US)
69# | | 16.08.1988 (de_DE)
70# ------------------------------------------------------------------------
71# %X | Locale’s appropriate time | 21:30:00 (en_US);
72# | representation. | 21:30:00 (de_DE)
73# ------------------------------------------------------------------------
74# %% | A literal '%' character. | %
1| Directive | Meaning | Example |
2|-----------|------------------------------------------------------------------------------------------|
3|%a | Abbreviated weekday name. | Sun, Mon, .. |
4|%A | Full weekday name. | Sunday, Monday, ... |
5|%w | Weekday as a decimal number. | 0, 1, ..., 6 |
6|%d | Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
7|%-d | Day of the month as a decimal number. | 1, 2, ..., 30 |
8|%b | Abbreviated month name. | Jan, Feb, ..., Dec |
9|%B | Full month name. | January, February, ... |
10|%m | Month as a zero-padded decimal number. | 01, 02, ..., 12 |
11|%-m | Month as a decimal number. | 1, 2, ..., 12 |
12|%y | Year without century as a zero-padded decimal number. | 00, 01, ..., 99 |
13|%-y | Year without century as a decimal number. | 0, 1, ..., 99 |
14|%Y | Year with century as a decimal number. | 2013, 2019 etc. |
15|%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
16|%-H | Hour (24-hour clock) as a decimal number. | 0, 1, ..., 23 |
17|%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
18|%-I | Hour (12-hour clock) as a decimal number. | 1, 2, ... 12 |
19|%p | Locale’s AM or PM. | AM, PM |
20|%M | Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
21|%-M | Minute as a decimal number. | 0, 1, ..., 59 |
22|%S | Second as a zero-padded decimal number. | 00, 01, ..., 59 |
23|%-S | Second as a decimal number. | 0, 1, ..., 59 |
24|%f | Microsecond as a decimal number, zero-padded on the left. | 000000 - 999999 |
25|%z | UTC offset in the form +HHMM or -HHMM. | |
26|%Z | Time zone name. | |
27|%j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
28|%-j | Day of the year as a decimal number. 1, 2, ..., 366 | |
29|%U | Week number of the year (Sunday as the first day of the week). | 00, 01, ..., 53 |
30|%W | Week number of the year (Monday as the first day of the week). | 00, 01, ..., 53 |
31|%c | Locale’s appropriate date and time representation. | Mon Sep 30 07:06:05 2013|
32|%x | Locale’s appropriate date representation. | 09/30/13 |
33|%X | Locale’s appropriate time representation. | 07:06:05 |
34|%% | A literal '%' character. | % |
35
1%a - Abbreviated weekday name. (Sun, Mon, ...)
2%A - Full weekday name. (Sunday, Monday, ...)
3%w - Weekday as a decimal number. (0, 1, ..., 6)
4%d - Day of the month as a zero-padded decimal. (01, 02, ..., 31)
5%-d - Day of the month as a decimal number. (1, 2, ..., 30)
6%b - Abbreviated month name. (Jan, Feb, ..., Dec)
7%B - Full month name. (January, February, ...)
8%m - Month as a zero-padded decimal number. (01, 02, ..., 12)
9%-m - Month as a decimal number. (1, 2, ..., 12)
10%y - Year without century as a zero-padded decimal number. (00, 01, ..., 99)
11%-y - Year without century as a decimal number. (0, 1, ..., 99)
12%Y - Year with century as a decimal number. (2013, 2019 etc.)
13%H - Hour (24-hour clock) as a zero-padded decimal number. (00, 01, ..., 23)
14%-H - Hour (24-hour clock) as a decimal number. (0, 1, ..., 23)
15%I - Hour (12-hour clock) as a zero-padded decimal number. (01, 02, ..., 12)
16%-I - Hour (12-hour clock) as a decimal number. (1, 2, ... 12)
17%p - Locale’s AM or PM. (AM, PM)
18%M - Minute as a zero-padded decimal number. (00, 01, ..., 59)
19%-M - Minute as a decimal number. (0, 1, ..., 59)
20%S - Second as a zero-padded decimal number. (00, 01, ..., 59)
21%-S - Second as a decimal number. (0, 1, ..., 59)
22%f - Microsecond as a decimal number, zero-padded on the left. (000000 - 999999)
23%z - UTC offset in the form +HHMM or -HHMM.
24%Z - Time zone name.
25%j - Day of the year as a zero-padded decimal number. (001, 002, ..., 366)
26%-j - Day of the year as a decimal number. (1, 2, ..., 366)
27%U - Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. (00, 01, ..., 53)
28%W - Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. (00, 01, ..., 53)
29%c - Locale’s appropriate date and time representation. (Mon Sep 30 07:06:05 2013)
30%x - Locale’s appropriate date representation. (09/30/13)
31%X - Locale’s appropriate time representation. (07:06:05)
32%% - A literal '%' character. (%)