str到python的时间 [英] str to time in python

查看:125
本文介绍了str到python的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

time1 = "2010-04-20 10:07:30"
time2 = "2010-04-21 10:07:30"

如何将上述字符串转换为时间戳?

How to convert the above from string to time stamp?

需要减去上述时间戳 time2-time1

推荐答案

2.5 +

from datetime import datetime
format = '%Y-%m-%d %H:%M:%S'
print datetime.strptime(time2, format) - 
        datetime.strptime(time1, format)
# 1 day, 0:00:00

编辑:为Python 2.4

for Python 2.4

import time
format = '%Y-%m-%d %H:%M:%S'
print time.mktime(time.strptime(time2, format)) - 
        time.mktime(time.strptime(time1, format))
# 86400.0

这篇关于str到python的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆