Open In App
Related Articles

Python DateTime – Time Class

Improve Article
Save Article
Like Article

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.


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]



# Python program to
# demonstrate time class
from datetime import time
# calling the constructor
my_time = time(12, 14, 36)
print("Entered time", my_time)
# calling constructor with 1
# argument
my_time = time(minute = 12)
print("\nTime with one argument", my_time)
# Calling constructor with
# 0 argument
my_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

Class Attributes

Let’s see the attributes provided by this class – 

Attribute NameDescription
minMinimum possible representation of time
maxMaximum possible representation of time
resolutionThe minimum possible difference between time objects
hourThe range of hour must be between 0 and 24 (not including 24)
minuteThe range of minute must be between 0 and 60 (not including 60)
secondThe range of second must be between 0 and 60 (not including 60)
microsecondThe range of microsecond must be between 0 and 1000000 (not including 1000000)
tzinfoThe object containing timezone information
foldRepresents if the fold has occurred in the time or not

Example 1: Getting min and max representable time


from datetime import time
# Getting min time
mintime = time.min
print("Min Time supported", mintime)
# Getting max time
maxtime = time.max
print("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 


from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
# Accessing Attributes
print("Hour:", Time.hour)
print("Minutes:", Time.minute)
print("Seconds:", Time.second)
print("Microseconds:", Time.microsecond)


Hour: 12
Minutes: 24
Seconds: 36
Microseconds: 1212

Class Functions

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. 

List of Time Class Functions

Function NameDescription
dst()Returns tzinfo.dst() is tzinfo is not None
fromisoformat()Returns a time object from the string representation of the time
isoformat()Returns the string representation of time from the time object
replace()Changes the value of the time object with the given parameter
strftime()Returns a string representation of the time with the given format
tzname()Returns tzinfo.tzname() is tzinfo is not None
utcoffset()Returns tzinfo.utcffsets() is tzinfo is not None

Let’s see certain examples of the above functions

Example 1: Converting time object to string and vice versa 


from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
# Converting Time object to string
Str = Time.isoformat()
print("String Representation:", Str)
Time = "12:24:36.001212"
# Converting string to Time object
Time = time.fromisoformat(Str)
print("\nTime from String", Time)


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 


from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
print("Original time:", Time)
# Replacing hour
Time = Time.replace(hour = 13, second = 12)
print("New Time:", Time)
# Formatting Time
Ftime = 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


Last Updated : 03 Sep, 2021
Like Article
Save Article
Similar Reads
Related Tutorials