22

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”

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?

200_success
  • 7,286
  • 1
  • 43
  • 74
Mark K
  • 8,767
  • 14
  • 58
  • 118
  • 1
    possible duplicate of [Understanding timedelta](http://stackoverflow.com/questions/6749294/understanding-timedelta) – metatoaster Aug 22 '14 at 03:56

3 Answers3

36

Use a relativedelta from the dateutil package. This will take into account leap years and other quirks.

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".

mhawke
  • 84,695
  • 9
  • 117
  • 138
10

diff is a timedelta instance.

for python2, see: https://docs.python.org/2/library/datetime.html#timedelta-objects

for python 3, see: https://docs.python.org/3/library/datetime.html#timedelta-objects

from docs:

timdelta instance attributes (read-only):

  • days
  • seconds
  • microseconds

timdelta instance methods:

  • total_seconds()

timdelta class attributes are:

  • min
  • max
  • resolution

You can use the days and seconds instance attributes to calculate what you need.

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)
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • thanks, Corey Goldberg. your original reply is very useful too. – Mark K Aug 22 '14 at 06:33
  • 1
    The example is not working, it says the difference between the two date is just 19 hours ... obviously, '2014-05-06' and '2013-03-06' is not the case. – tong Jul 30 '18 at 03:52
  • Tong, since he used `seconds`, that excludes the amount calculated with `days`, so it would be `diff.days` ( = 425) plus that 19 hours – Chris Aug 03 '22 at 19:08
1

To calculate the difference between the timestamps:

from time import time

def timestamp_from_seconds(seconds):
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    return days, hours, minutes, seconds

print("\n%d days, %d hours, %d minutes, %d seconds" % timestamp_from_seconds(abs(1680375128- time())))

output: 1 days, 19 hours, 19 minutes, 55 seconds

Abolfazl Rastgou
  • 655
  • 10
  • 13