In an earlier article we have seen how to generate a date value form separate day, month and year values. Here we will see how to generate a time value from separate hours, minutes and seconds variables. The easiest way to generate time from it’s individual components is by using the datetime.time() object. In this method we will use the datetime module’s time() object with it’s optional arguments. We can pass the values of hour, minutes, seconds and microseconds as arguments to generate a time of type datetime.time.
import datetime
hours = 19
minutes = 50
seconds = 43
microseconds = 123
time_1 = datetime.time(hours, minutes, seconds, microseconds)
print("\n", time_1, "\n")
print("\n", type(time_1), "\n")
Reference
- About datetime.time() method in Python Docs.