我有两个日期字符串(从用户输入中取得,可能差异很大)
s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'
我希望能够找出两者之间的差异并以分钟为单位表示。
我应该如何处理这个问题呢?
我有两个日期字符串(从用户输入中取得,可能差异很大)
s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'
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
>>> from datetime import datetime
>>> s1 = '2011:10:01:10:30:00'
>>> s2 = '2011:10:01:11:15:00'
>>> d1=datetime.strptime(s1,'%Y:%m:%d:%I:%M:%S')
>>> d2=datetime.strptime(s2,'%Y:%m:%d:%I:%M:%S')
>>> d2-d1
datetime.timedelta(0, 2700)
>>> (d2-d1).total_seconds()/60
45.0
time
模块可以对此有所帮助。
import time
s1 = '2011:10:01:10:30:00'
s2 = '2011:10:01:11:15:00'
s1Time = time.strptime(s1, "%Y:%m:%d:%H:%M:%S")
s2Time = time.strptime(s2, "%Y:%m:%d:%H:%M:%S")
deltaInMinutes = (time.mktime(s2Time) - time.mktime(s1Time)) / 60.0
print deltaInMinutes, "minutes"