2

I am trying to print the approximate time difference between two dates. In the really well answered question here: Format timedelta to string several answers were given, and I can use one of those to solve my issue.

However, I really liked the humanize approach. Unfortunately I can not get it to work, since the minimum_unit keyword argument, which is listed in the documentation, gives me an error:

import datetime as dt
import humanize as hum
d1=dt.datetime(2003,3,17)
d2=dt.datetime(2007,9,21)
hum.naturaldelta(d2-d1, minimum_unit="days")

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-238c3a390a42> in <module>()
      3 d1=dt.datetime(2003,3,17)
      4 d2=dt.datetime(2007,9,21)
----> 5 hum.naturaldelta(d2-d1, minimum_unit="days")

TypeError: naturaldelta() got an unexpected keyword argument 'minimum_unit'

Note: the months=True argument doesnt help, because it only forces the timedelta to be returned in months instead of days, when the difference is below one year.

Any ideas what I am doing wrong? (If this is just not possible, then I will use some workaround.)

EDIT:

I am using https://colab.research.google.com/drive/, which seems to run Python "3.7.10 (default, Feb 20 2021, 21:17:23) [GCC 7.5.0]"

EDIT/SOLUTION:

Sorry, I was stupid, but I will leave the question. If someone wants to remove it, no objections. The comment by MrFuppes helped me realize that it was mostly due to Google not using the current version. Indeed, after checking with pip list, I saw that only 0.x version was installed, while 3.x was current. After running pip install humanize --upgrade I was able to use the precisedelta function suggested in the accepted answer.

Maxim Moloshenko
  • 295
  • 2
  • 10
  • 1
    I get a different error (with same result though): `ValueError: Minimum unit 'days' not supported`. In the src code, the error is [raised here](https://github.com/jmoiron/humanize/blob/77d083f22e161ad2d2bf2a77056977e7b9dbae6e/src/humanize/time.py#L112). To me, this looks like a hack/work-around, not sure why it is there. Maybe you should [raise an issue](https://github.com/jmoiron/humanize/issues). – MrFuppes Mar 24 '21 at 10:00
  • Do months work for you as minimum_unit? – Maxim Moloshenko Mar 24 '21 at 10:06
  • Nope, just seconds or smaller (higher resolution). and it doesn't change the output; just says "4 years". This is what I would expect from setting a *granularity* to "years". Room for improvement I'd say. – MrFuppes Mar 24 '21 at 10:11
  • Your comments helped a lot in figuring out the problem, thanks a lot! I added an edit to my post. – Maxim Moloshenko Mar 24 '21 at 10:32

1 Answers1

2

Use humanfriendly

import datetime
import humanfriendly

d1 = datetime.datetime(2003, 3, 17)
d2 = datetime.datetime(2007, 9, 21)
date_delta = d2 - d1

# there is no month
humanfriendly.format_timespan(date_delta)
>>> '4 years, 27 weeks and 4 days'

Or maybe this:

from humanize.time import precisedelta

precisedelta(date_delta, minimum_unit='days')
>>> '4 years, 6 months and 5.84 days'
precisedelta(d2-d1, minimum_unit='days', suppress=['months'])
>>> '4 years and 188.84 days'
precisedelta(d2-d1, minimum_unit='days', format="%0.0f")
>>> '4 years, 6 months and 6 days'
Johnny
  • 652
  • 1
  • 3
  • 21
  • Thanks! I switched to precisedelta. Also found that my issue was due to google using an outdated package, which seems super weird to me, but whatever. – Maxim Moloshenko Mar 24 '21 at 10:20