T
T
Tema_Kyklovod2020-04-17 00:37:27
Python
Tema_Kyklovod, 2020-04-17 00:37:27

How to calculate from date to date?

Krch date
07/31/2017
We need to calculate until now the
output in the format
Years months days have passed
I know that it's easy, but I have errors in my calculations,
please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-04-17
@Tema_Kyklovod

>>> import datetime
>>> from dateutil import relativedelta
>>> past_date = '2017-07-31'
>>> past_date = datetime.datetime.strptime(past_date, '%Y-%m-%d')
>>> today = datetime.datetime.now()
>>> diff = relativedelta.relativedelta(today, past_date)
>>> f'{diff.years} years, {diff.months} months and {diff.days} days'
'2 years, 8 months and 17 days'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question