我给了像这样的日期字符串:
Mon Jun 28 10:51:07 2010 Fri Jun 18 10:18:43 2010 Wed Dec 15 09:18:43 2010
什么是方便的python方法来计算天数差异?假设时区相同。
字符串由linux命令返回。
编辑:谢谢,这么多好的答案
#!/usr/bin/env python import datetime def hrdd(d1, d2): """ Human-readable date difference. """ _d1 = datetime.datetime.strptime(d1, "%a %b %d %H:%M:%S %Y") _d2 = datetime.datetime.strptime(d2, "%a %b %d %H:%M:%S %Y") diff = _d2 - _d1 return diff.days # <-- alternatively: diff.seconds if __name__ == '__main__': d1 = "Mon Jun 28 10:51:07 2010" d2 = "Fri Jun 18 10:18:43 2010" d3 = "Wed Dec 15 09:18:43 2010" print hrdd(d1, d2) # ==> -11 print hrdd(d2, d1) # ==> 10 print hrdd(d1, d3) # ==> 169 # ...
>>> import datetime >>> a = datetime.datetime.strptime("Mon Jun 28 10:51:07 2010", "%a %b %d %H:%M:%S %Y") >>> b = datetime.datetime.strptime("Fri Jun 18 10:18:43 2010", "%a %b %d %H:%M:%S %Y") >>> c = a-b >>> c.days 10