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!