Human datetime diffs with Pendulum

2.5k Views Asked by At

I've recently stumbled upon this awesome pendulum "datetimes made easy" library.

It has a very nice feature of displaying "human like" diffs between datetimes:

In [1]: import pendulum

In [2]: now = pendulum.now()

In [3]: future = now.add(years=10)

In [4]: future.diff_for_humans()
Out[4]: '10 years from now'

But, is it possible to make it work for a more complicated difference - say "years" and "weeks"?

In [5]: future = now.add(years=10, weeks=5)

In [6]: future.diff_for_humans()
Out[6]: '10 years from now'

I would expect it to output 10 years and 5 weeks from now.

1

There are 1 best solutions below

6
On BEST ANSWER

From the Pendulum module readme:

now = pendulum.now()
future = now.add(years=10, weeks=5)
delta = future - now
delta.in_words()
>>>'10 years 1 month 4 days'

https://github.com/sdispater/pendulum