ID EN
Datetime

astimezone

Python 3.11

Return a datetime object with new tzinfo attribute tz, adjusting the date and time data so the result is the same UTC time as self, but in tz’s local time.

Syntax

PYTHON
datetime.astimezone(tz=None)

Examples

Example 1
PYTHON
def astimezone(self, tz):
    if self.tzinfo is tz:
        return self
     Convert self to UTC, and attach the new time zone object.
    utc = (self - self.utcoffset()).replace(tzinfo=tz)
     Convert from UTC to tz's local time.
    return tz.fromutc(utc)

See Also

datetime tzinfo tzinfo utcoffset() dst() timezone tzinfo.fromutc() tzinfo