Home > Mobile >  How to compare two dates in python
How to compare two dates in python

Time:11-28

I have 2 dates i want the no of days between the 2 dates how to retreve that

expiry_date=details['expires']
today=date.today().strftime("%d/%m/%Y")

for example 28/11/2021 29/11/2021 no.of.days=1

CodePudding user response:

If you have two datetime objects, you can subtract them to get a timedelta.

diff = (datetime.datetime.strptime("29/11/2021", "%d/%m/%Y") - 
        datetime.datetime.strptime("28/11/2021", "%d/%m/%Y"))

You can then convert to days:

diff.total_seconds() / 86400.

CodePudding user response:

Search for the answer yourself first, then ask if you can't find anything.

How to calculate number of days between two given dates

Use this

  • Related