本文主要是介绍python 字符串转datetime 时间戳转datetime 字符转转时间戳,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# 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 datetimek = 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 times = '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] = dtprint dt_str, dt
这篇关于python 字符串转datetime 时间戳转datetime 字符转转时间戳的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!