ADVERTISEMENT
ADVERTISEMENT

Python datetime timetz() Method with Example

Python datetime.timetz() Method: Here, we are going to learn about the timetz() method of datetime class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 03, 2020

Python datetime.timetz() Method

datetime.timetz() method is used to manipulate objects of datetime class of module datetime.

It uses an instance method and converts it and returns a time object with the same hour, minute, second, microsecond, and fold and tzinfo attributes. Unlike datetime.timetz() method the tzinfo is not None here if present in the original datetime instance object.

Module:

    import datetime

Class:

    from datetime import datetime

Syntax:

    timetz()

Parameter(s):

  • None

Return value:

The return type of this method is a time object with same hour, minute, second, microsecond, fold and tzinfo.

Example:

## Creating a time object from a datetime object
import datetime
## Creating datetime instance
x = datetime.datetime(2020, 3, 4, 23, 12, 23, 44)
d = x.timetz()
print("Original object:", x)
print("New time object:",d)
print()

x = datetime.datetime.now()
d = x.timetz()
print("Original date and time", x)
print("New time object:", d)
print()

t = datetime.timedelta(hours=5, minutes= 30)
tzobj = datetime.timezone(t, name="IST")
x = datetime.datetime(2020, 3, 22, 12, 23, 34, 45, tzobj)
d = x.timetz()
print("Original datetime object:", x)
print("Time object with tzinfo attributes:", d)
print("Time object without tzinfo attributes:", x.time())

Output

Original object: 2020-03-04 23:12:23.000044
New time object: 23:12:23.000044

Original date and time 2020-05-03 18:26:35.437067
New time object: 18:26:35.437067

Original datetime object: 2020-03-22 12:23:34.000045+05:30
Time object with tzinfo attributes: 12:23:34.000045+05:30
Time object without tzinfo attributes: 12:23:34.000045
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.