Python计算时差,在1中给出“年、月、日、时、分和秒”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25439279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python calculating time difference, to give ‘years, months, days, hours, minutes and seconds’ in 1
提问by Mark K
I want to know how many years, months, days, hours, minutes and seconds in between '2014-05-06 12:00:56' and '2012-03-06 16:08:22'. The result shall looked like: “the difference is xxx year xxx month xxx days xxx hours xxx minutes”
我想知道“2014-05-06 12:00:56”和“2012-03-06 16:08:22”之间有多少年、月、日、小时、分钟和秒。结果应如下所示:“差异为 xxx 年 xxx 月 xxx 天 xxx 小时 xxx 分钟”
For example:
例如:
import datetime
a = '2014-05-06 12:00:56'
b = '2013-03-06 16:08:22'
start = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
ends = datetime.datetime.strptime(b, '%Y-%m-%d %H:%M:%S')
diff = start – ends
if I do:
如果我做:
diff.days
It gives the difference in days.
它给出了天数的差异。
What else I can do? And how can I achieve the wanted result?
我还能做什么?我怎样才能达到想要的结果?
采纳答案by mhawke
Use a relativedeltafrom the dateutil package. This will take into account leap years and other quirks.
使用relativedelta从dateutil包。这将考虑闰年和其他怪癖。
import datetime
from dateutil.relativedelta import relativedelta
a = '2014-05-06 12:00:56'
b = '2013-03-06 16:08:22'
start = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
ends = datetime.datetime.strptime(b, '%Y-%m-%d %H:%M:%S')
diff = relativedelta(start, ends)
>>> print "The difference is %d year %d month %d days %d hours %d minutes" % (diff.years, diff.months, diff.days, diff.hours, diff.minutes)
The difference is 1 year 1 month 29 days 19 hours 52 minutes
You might want to add some logic to print for e.g. "2 years" instead of "2 year".
您可能想要添加一些逻辑来打印例如“2 年”而不是“2 年”。
回答by Corey Goldberg
diff is a timedeltainstance.
diff 是一个timedelta实例。
for python2, see: https://docs.python.org/2/library/datetime.html#timedelta-objects
对于 python2,请参见:https: //docs.python.org/2/library/datetime.html#timedelta-objects
for python 3, see: https://docs.python.org/3/library/datetime.html#timedelta-objects
对于 python 3,请参见:https: //docs.python.org/3/library/datetime.html#timedelta-objects
from docs:
来自文档:
timdelta instance attributes (read-only):
timdelta 实例属性(只读):
- days
- seconds
- microseconds
- 天
- 秒
- 微秒
timdelta instance methods:
timdelta 实例方法:
- total_seconds()
- total_seconds()
timdelta class attributes are:
timdelta 类属性是:
- min
- max
- resolution
- 分钟
- 最大限度
- 解析度
You can use the daysand secondsinstance attributes to calculate what you need.
您可以使用days和seconds实例属性来计算您需要的内容。
for example:
例如:
import datetime
a = '2014-05-06 12:00:56'
b = '2013-03-06 16:08:22'
start = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
ends = datetime.datetime.strptime(b, '%Y-%m-%d %H:%M:%S')
diff = start - ends
hours = int(diff.seconds // (60 * 60))
mins = int((diff.seconds // 60) % 60)

