Python DateTime - Time Class - GeeksforGeeks

03 Sep, 2021
Time class represents the local time of the day which is independent of any particular day. This class can have the tzinfo object which represents the timezone of the given time. If the tzinfo is None then the time object is the naive object otherwise it is the aware object.
Syntax:
class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
All the arguments are optional. tzinfo can be None otherwise all the attributes must be integer in the following range –
0 <= hour < 24
0 <= minute < 60
0 <= second < 60
0 <= microsecond < 1000000
fold in [0, 1]
Example:
Python3
# Python program to# demonstrate time class from datetime import time # calling the constructormy_time = time(12, 14, 36) print("Entered time", my_time) # calling constructor with 1# argumentmy_time = time(minute = 12)print("\nTime with one argument", my_time) # Calling constructor with# 0 argumentmy_time = time()print("\nTime without argument", my_time) # Uncommenting time(hour = 26)# will rase an ValueError as# it is out of range # uncommenting time(hour ='23')# will raise TypeError as# string is passed instead of int
Entered time 12:14:36

Time with one argument 00:12:00

Time without argument 00:00:00
Let’s see the attributes provided by this class – 
Example 1: Getting min and max representable time
Python3
from datetime import time # Getting min timemintime = time.minprint("Min Time supported", mintime) # Getting max timemaxtime = time.maxprint("Max Time supported", maxtime)
Min Time supported 00:00:00
Max Time supported 23:59:59.999999
Example 2: Accessing the hour, minutes, seconds, and microseconds attribute from the time class 
Python3
from datetime import time # Creating Time objectTime = time(12,24,36,1212) # Accessing Attributesprint("Hour:", Time.hour)print("Minutes:", Time.minute)print("Seconds:", Time.second)print("Microseconds:", Time.microsecond)
Hour: 12
Minutes: 24
Seconds: 36
Microseconds: 1212
Time class provides various functions like we can get time from string or convert time to string, format the time according to our need, etc. Let’s see a list of all the functions provided by the time class. 
Let’s see certain examples of the above functions
Example 1: Converting time object to string and vice versa 
Python3
from datetime import time # Creating Time objectTime = time(12,24,36,1212) # Converting Time object to stringStr = Time.isoformat()print("String Representation:", Str)print(type(Str)) Time = "12:24:36.001212" # Converting string to Time objectTime = time.fromisoformat(Str)print("\nTime from String", Time)print(type(Time))
Output 
String Representation: 12:24:36.001212
<class 'str'>

Time from String 12:24:36.001212
<class 'datetime.time'>
Example 2: Changing the value of an already created time object and formatting the time 
Python3
from datetime import time # Creating Time objectTime = time(12,24,36,1212)print("Original time:", Time) # Replacing hourTime = Time.replace(hour = 13, second = 12)print("New Time:", Time) # Formatting TimeFtime = Time.strftime("%I:%M %p")print("Formatted time", Ftime)
Original time: 12:24:36.001212
New Time: 13:24:12.001212
Formatted time 01:24 PM
Note: For more information on Python Datetime, refer to Python Datetime Tutorial
 
abhishek0719kadiyan
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list