Skip to content

Time

Time representation

Code

from datetime import time

Create

Action Code Details
From hms
time(hour, minute, second)
Current time
datetime.now().time()
Time from any valid ISO 8601 time string
time.fromisoformat(time_str)
e.g. 23:59:01
From total seconds since start of day
(datetime.min + timedelta(seconds=7321.5)).time()

Constants

Action Code Details
Latest representable time
time.max

Test

Action Code Details
Is time
isinstance(x, datetime.time)
Same moment in time
x == y
Up to x.resolution precision (usually 1Ξs)
Happens before y
x < y
Happens after y
x > y

Extract

Action Code Details
Hour
x.hour
Minute
x.minute
Second
x.second
Microsecond
x.microsecond
Smallest representable time difference
time.resolution

Convert

Action Code Details
Total seconds
?
Format time as ISO 8601
x.isoformat()
e.g., 23:59:00
Format datetime as HH:mm
x.strftime('%H:%M')
e.g., 23:59
Format datetime as HH:mm:ss
x.strftime('%H:%M:%S')
e.g., 23:59:00
Format readable datetime of consistent length
x.ctime()
e.g., Sun Jan 1 23:59:00 2023
Named tuple
x.timetuple()