简体   繁体   中英

How to compare two dates in python

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM