python如何进行时间戳与日期格式互换?
1、倒入需要的工具包。
import time

2、现在时间戳格式为:start=1535731200。
利用time包的函数localtime将其转换为日期。
start_trans=time.localtime(start)
print(start_trans)

3、我们要需要将以上的系统日期转换为需要的格式。如(%Y-%m-%d %H:%M:%S)
start_trans_2=time.strftime('%Y-%m-%d %H:%M:%S',start_trans)
print(start_trans_2)
start_trans_3=time.strftime('%Y-%m-%d',start_trans)
print(start_trans_3)
以上运行结果分别为:
2018-09-01 00:00:00
2018-09-01

4、将日期格式转换为时间戳。
now="2018-09-01 00:00:00"
将字符串转换为标准时间结构
now=time.strptime(now, "%Y-%m-%d %H:%M:%S")
print(now)

5、将标准时间结构转换为时间戳
now_trans=time.mktime(now)
print(now_trans)

6、利用int函数将上述结果取整即可。
now_trans=int(now_trans)
print(now_trans)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:79
阅读量:78
阅读量:175
阅读量:155
阅读量:191