python 时间处理
python 时间处理
目录
前言
最近写的python代码要处理各种类型的时间戳,每遇到不同的场景就要去查一次,就干脆整理了一份各种场景下的转换,遇到了就来查询。(注:以下代码基于python 3.6)
获取当前时间
当前时间戳 (13位)
# 1536202181120
int(round(time.time() * 1000))
round()是四舍五入
当前时间戳 (10位)
# 1536203002
int(round(time.time()))
当前时间(带毫秒)
# 2018-09-06 10:53:11.518
str(datetime.datetime.now())[:-3]
当前时间(不带毫秒)
# 2018-09-06 10:53:11
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
时间戳 转 时间
时间戳(10位) 转 时间(不带毫秒)
# 1536203002 -> 2018-09-06 11:03:22
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int('1536203002')))
时间戳(13位) 转 时间(不带毫秒)
# 1536203002111 -> 2018-09-06 11:03:22
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int('1536203002111'[:-3])))
时间戳(13位) 转 时间(带毫秒)
# 1536203002111 -> 2018-09-06 11:03:22.111
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int('1536203002111'[:-3]))) + '.'+'1536203002111'[-3:]
时间 转 时间戳
时间(不带毫秒) 转 时间戳(10位)
# 2018-09-06 11:03:22 -> 1536203002
int(time.mktime(time.strptime('2018-09-06 11:03:22', "%Y-%m-%d %H:%M:%S")))
时间(带毫秒) 转 时间戳(10位)
# 2018-09-06 11:03:22.111 -> 1536203002
int(round(time.mktime(time.strptime('2018-09-06 11:03:22.111'[:-4], "%Y-%m-%d %H:%M:%S"))))
时间(带毫秒) 转 时间戳(13位)
# 2018-09-06 11:03:22.111 -> 1536203002111
int(round(datetime.datetime.strptime('2018-09-06 11:03:22.111', "%Y-%m-%d %H:%M:%S.%f").timestamp()*1000))
powered by mujiang