Python will be a known utc time string into eight districts in Eastern Time

A first acquisition time format utc

utc_time = datetime.datetime.utcnow()

print(utc_time)

Export

2018-06-24T08:59:39Z

PS: I encountered a problem no one answer? Requires Python learning materials? Click on the link below you can add yourself get
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76

Here we assume that currently have is a UTC time format string, then how to convert it to the East eight districts in time?

utc_time = "2018-06-24T08:59:39Z"

1. Remove wherein the letters

# 第一次替换为空格,第二次替换为空字符串
utc_time = utc_time.replace("T", " ").replace("Z","")

2. time to convert a string array

# 是strptime 不是 strftime  二者之间的作用相反

import time

utc_time = time.strptime(utc_time, "%Y-%m-%d %H:%M:%S")

3. The conversion time timestamp array

 utc_time = time.mktime(utc_time)

4. timestamp to a timestamp East eight districts

beijing_time = utc_time + 8*60*60

5. The stamp can be formatted

new_time = time.strftime('%Y-%m-%d %H:%M:%S', beijing_time)

At this time you get a new East eight districts of the string, and is so simple

Guess you like

Origin www.cnblogs.com/python960410445/p/12093487.html