# coding:utf8
import datetime
# 字符串转datetime
s = '2018-10-08 23:08:18'
dt = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
print dt
# datetime转字符串
s = dt.strftime('%Y%m%d')
print s
import datetime
k = 1658066400
# 北京时间,应该是当地时区的时间
dateArray = datetime.datetime.fromtimestamp(k)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime) # 2022-07-17 22:00:00
# UTC时间
dateArray = datetime.datetime.utcfromtimestamp(k)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime) # 2022-07-17 14:00:00
import time
s = '2018-10-08 23:08:18'
time_stamp = int(time.mktime(time.strptime(s, "%Y-%m-%d %H:%M:%S")))
print time_stamp
# coding:utf8
import datetime
# 构造字符串到dt的映射
dt_dic = {}
start_dt = datetime.datetime.strptime('20220130', '%Y%m%d')
for i in range(7):
dt = start_dt + datetime.timedelta(days=i)
dt_str = dt.strftime('%Y%m%d')
dt_dic[dt_str] = dt
print dt_str, dt