1import time
2
3start = time.time()
4print("hello")
5end = time.time()
6print(end - start)
7
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
1t = time.localtime() # Gets the local time
2current_time = time.strftime("%H:%M", t) # Gets the time in the desired format
3current_time = "The time is " + current_time
1
2from datetime import datetime
3
4timestamp = 1528797322
5date_time = datetime.fromtimestamp(timestamp)
6
7print("Date time object:", date_time)
8
9d = date_time.strftime("%m/%d/%Y, %H:%M:%S")
10print("Output 2:", d)
11
12d = date_time.strftime("%d %b, %Y")
13print("Output 3:", d)
14
15d = date_time.strftime("%d %B, %Y")
16print("Output 4:", d)
17
18d = date_time.strftime("%I%p")
19print("Output 5:", d)
20
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#import time module:
2import time
3#module has various attributes:
4dir(time)
5[..., 'localtime', 'mktime', 'sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us', 'time']
6
7#default expression is in seconds with zero being start of runtime
8secFromStart = time.time()