Answer the question
In order to leave comments, you need to log in
How to correctly calculate time between datetime dates?
I have the following date, for example: 2006-10-01 (yyyy-mm-dd).
I need to calculate the number of years, months and days between a given date and the current one.
I tried many options, but for some reason datetime does not take into account leap years.
Here is an example of my code:
date = "2006-09-23"
date_object = datetime.datetime.strptime(date, "%Y-%m-%d")
delta = datetime.datetime.now() + datetime.timedelta(seconds=-date_object.timestamp()
return 'Аккаунт зарегистрирован: ' + date + '\n' + str(int(delta.strftime('%Y')) - 1970) + ' лет, ' + str(int(delta.strftime('%m')) - 1) + ' месяцев, ' + str(int(delta.strftime('%d')) - 1) + ' дней.'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question