python时间,时间戳的转化与处理

来源:互联网 发布:javascript插件dll 编辑:程序博客网 时间:2024/05/17 03:52

1.时间戳转化为时间

timestamp= 1502508104707timeArray = time.localtime(timeStamp)otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

2.获取当前时间并转化为指定格式


import timenow =int(time.time()) #当前时间戳timeArray= time.localtime(now)

3.计算两个时间的差的秒数

time1 = datetime.datetime(2017,8,4,1,47,20)time2 = datetime.datetime(2017,8,12,5,25,25)substract=(time2-time1).total_seconds()


4.日期字符串转化为time对象

timeStr='2017-8-10 12:20:26'timeObj = datetime.datetime.strptime(timeStr, "%Y-%m-%d %H:%M:%S")