Python converts UTC time to local time

Source: Internet
Author: User

Recently, I encountered a problem in the project: the database storage time is UTC time, but now it is required that the display time is local time, because I have little access to Python before, I am not familiar with the datetime operation in Python. After reading the relevant information on the Internet, I finally succeeded in solving this problem. The method is as follows:

From dateutil import tzfrom datetime import datetime # UTC zonefrom_zone = tz. gettz ('utc') # China zoneto_zone = tz. gettz ('cst ') UTC = datetime. utcnow () # Tell the datetime object that it's in UTC time zoneutc = UTC. replace (tzinfo = from_zone) # convert time zonelocal = UTC. astimezone (to_zone) print datetime. strftime (local, "% Y-% m-% d % H: % m: % s ")

For how to obtain the code for the local time zone, refer to the following code:

From datetime import * From dateutil. TZ import * print datetime. Now (tzlocal (). tzname ()

Over!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.