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# Here is a self updating clock function, just run it and it'll self update itself until you hit CTRL-C
2import datetime
3import time
4def clock():
5 while True:
6 print(datetime.datetime.now().strftime("%H:%M:%S"), end="\r")
7 time.sleep(1)
8
9clock()
1>>> from time import perf_counter
2>>> def longrunning_function():
3... for i in range(1, 11):
4... time.sleep(i / i ** 2)
5...
6>>> start = perf_counter()
7>>> longrunning_function()
8>>> end = perf_counter()
9>>> execution_time = (end - start)
10>>> execution_time
118.201258441999926
12
1>>> strptime('Fri Mar 01 23:38:40 2019')
2time.struct_time(tm_year=2019, tm_mon=3, tm_mday=1, tm_hour=23, tm_min=38, tm_sec=40, tm_wday=4, tm_yday=60, tm_isdst=-1)
3