码迷,mamicode.com
首页 > 编程语言 > 详细

python添加时间戳

时间:2018-09-13 20:12:07      阅读:657      评论:0      收藏:0      [点我收藏+]

标签:span   local   lam   htm   pytho   code   str   ftime   日期   

#时间戳日期
#参考:https://www.cnblogs.com/fangbei/p/python-time.html

import time
import datetime


#获取秒级时间戳与毫秒级时间戳
t = time.time()

print(t)                       #原始时间数据
print(int(t))                  #秒级时间戳
print(int(round(t * 1000)))    #毫秒级时间戳

nowTime = lambda:int(round(t * 1000))
print(nowTime());              #毫秒级时间戳,基于lambda

#获取当前日期和时间,类型:datetime
now_time = datetime.datetime.now()
print(type(now_time))

#日期格式化--datetime->字符串
dt = datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S)
print(dt)

#日期格式化--字符串->datetime
tt = datetime.datetime.strptime(dt, %Y-%m-%d %H:%M:%S)
print(type(tt))
print(tt)

#当前加一个小时
add_hour = now_time+datetime.timedelta(hours=1)
print(add_hour)

#将日期转为秒级时间戳
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print(ts)

#将秒级时间戳转为日期
dt2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt2)

 

python添加时间戳

标签:span   local   lam   htm   pytho   code   str   ftime   日期   

原文地址:https://www.cnblogs.com/lijinglj/p/9642286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!