有时候业务需要,需要把正常的时间格式与unix时间戳格式进行转换。
在python中转化方式如下,直接利用time中的函数:
#! /usr/bin/env python
#coding:utf-8
import sys,os,re
import time
def timestamp_datatime(value):
format = '%Y-%m-%d %H:%M'
#format = '%Y-%m-%d %H:%M:%S'
#value 为时间戳值,如:1460073600.0
value = time.localtime(value)
dt = time.strftime(format,value)
return dt
def datetime_timestamp(dt):
time.strptime(dt,'%Y-%m-%d %H:%M')
s = time.mktime(time.strptime(dt,'%Y-%m-%d %H:%M'))
return s
if __name__ == '__main__':
d = datetime_timestamp('2016-04-08 08:00')
print d
s = timestamp_datatime(1460073600.0)
print s
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有