Home > Backend Development > Python Tutorial > [python] convert UTC to your local time with standard libs.

[python] convert UTC to your local time with standard libs.

Barbara Streisand
Release: 2025-01-27 16:18:11
Original
637 people have browsed it

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:

  1. Create a datetime object containing UTC time zone information.
  2. Use the 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>
Copy after login

[python] convert UTC to your local time with standard libs.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template