Use the Python standard library to easily convert UTC time to local time
This article will introduce how to convert UTC time to your local time using Python's standard library. Just two easy steps:
astimezone
method to convert the previous datetime object. The following example demonstrates how to convert 2025-01-01T00:00Z to your local time:
<code class="language-python">from datetime import datetime from zoneinfo import ZoneInfo utc = datetime(2025, 1, 1, 0, 0, tzinfo=ZoneInfo("UTC")) localtime = utc.astimezone(tz=ZoneInfo("localtime")) print(localtime)</code>
Note:
In Windows systems and some minimal container environments, you may need to install the tzdata
package.
The above is the detailed content of [python] convert UTC to your local time with standard libs.. For more information, please follow other related articles on the PHP Chinese website!