Python datetime.timetz() Method with Example
The timetz() function manipulates the objects of the DateTime class of the DateTime module. This function uses an instance method given to it via reference, and converts them, and returns a time object with the same hour, minute, second, microsecond, and fold and tzinfo attributes.
Syntax: timetz()
Parameters: This function does not accept any parameter.
Return values: This function returns a time object with the same specified hour, minute, second, microsecond, fold and tzinfo.
Example 1: Python program to show application of timetz() function.
Python3
# Python3 code for getting# a time object with the same# specified hour, minute, second,# microsecond, fold and tzinfo. # Importing datetime moduleimport datetime # Creating a datetime instanceA = datetime.datetime(2021, 8, 3, 10, 11, 12, 13) # Calling the timetz() function over# the above datetime instanceB = A.timetz() # Printing the original date time objectprint("Original date time object:", A) # Printing the new time objectprint("New time object:", B) |
Original date time object: 2021-08-03 10:11:12.000013 New time object: 10:11:12.000013
Example 2: Python program to show the application of timetz() function.
Python3
# Python3 code for getting# a time object with the same# specified hour, minute, second,# microsecond, fold and tzinfo. # Importing datetime moduleimport datetime # Creating a current datetime instanceA = datetime.datetime.now() # Calling the timetz() function over# the above current datetime instanceB = A.timetz() # Printing the original current date time objectprint("Original current date time object:", A) # Printing the new current time objectprint("New current time object:", B) |
Original current date time object: 2021-08-05 07:41:24.147260 New current time object: 07:41:24.147260
Example 3: Python program to show application of timetz() function.
Python3
# Python3 code for getting# a time object with the same# specified hour, minute, second,# microsecond, fold and tzinfo. # Importing datetime moduleimport datetime # Creating datetime instancesA = datetime.timedelta(hours=12, minutes=12)obj = datetime.timezone(A, name="IST")B = datetime.datetime(2012, 1, 2, 3, 10, 15, 20, obj) # Calling the timetz() function over the above# specified datetimeC = B.timetz() # Printing the Original datetime objectprint("Original datetime object:", B) # Printing the time object with tzinfo attributesprint("Time object with tzinfo attributes:", C) # Printing the time object without tzinfo attributesprint("Time object without tzinfo attributes:", B.time()) |
Original datetime object: 2012-01-02 03:10:15.000020+12:12 Time object with tzinfo attributes: 03:10:15.000020+12:12 Time object without tzinfo attributes: 03:10:15.000020
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


