Python:两个timedate字符串的差 [英] python: difference of two timedate strings

查看:96
本文介绍了Python:两个timedate字符串的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期字符串(取自用户输入,并且可能相差很大)

I have two date strings (taken from user input and can vary greatly)

s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'

我希望以分钟为单位查找两者之间的差异。

I wish to find the difference between the two as minutes.

我应该如何解决这个问题?

How should I proceed to tackle this ?

推荐答案

import datetime

d1 = datetime.datetime.strptime('2011:10:01:10:30:00', '%Y:%m:%d:%H:%M:%S')
d2 = datetime.datetime.strptime('2011:10:01:11:15:00', '%Y:%m:%d:%H:%M:%S')
diff = (d2 - d1).total_seconds() / 60

如果您需要处理任意的日期时间格式,我相信内置的日期时间库不会为您做到这一点。也许可以查看以下内容:

If you need to handle arbitrary datetime formats, I don't believe the built in datetime library will do that for you. Perhaps check out something like:

http://www.egenix.com/products/python/mxBase/mxDateTime/

这篇关于Python:两个timedate字符串的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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