python 时间字符串相减
不标准的时间字符串,没有日期的,但是都是同一天的,比如'12:13:50'减'12:28:21'是多少秒怎么计算?还有,有什么现成的函数,能把秒转成00:00:00这种格...
不标准的时间字符串,没有日期的,但是都是同一天的,比如'12:13:50'减'12:28:21'是多少秒怎么计算?
还有,有什么现成的函数,能把秒转成00:00:00这种格式吗,或者n时n钟n秒,就是想知道两个时间的差,同时看起来直观些~ 展开
还有,有什么现成的函数,能把秒转成00:00:00这种格式吗,或者n时n钟n秒,就是想知道两个时间的差,同时看起来直观些~ 展开
2个回答
展开全部
$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> patt = re.compile(r'(?P<hour>\d{1,2})\:(?P<minute>\d{2})\:(?P<second>\d{2})')
>>> def strtime2second(s):
... try:
... d = patt.match(s).groupdict()
... return int(d['hour'])*3600+int(d['minute'])*60+int(d['second'])
... except:
... pass
...
>>> a = '12:13:50'
>>> b = '12:28:21'
>>> strtime2second(b) - strtime2second(a)
871
>>>
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> patt = re.compile(r'(?P<hour>\d{1,2})\:(?P<minute>\d{2})\:(?P<second>\d{2})')
>>> def strtime2second(s):
... try:
... d = patt.match(s).groupdict()
... return int(d['hour'])*3600+int(d['minute'])*60+int(d['second'])
... except:
... pass
...
>>> a = '12:13:50'
>>> b = '12:28:21'
>>> strtime2second(b) - strtime2second(a)
871
>>>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询