[英]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我有 2 个日期我想要 2 个日期之间的天数如何检索
expiry_date=details['expires']
today=date.today().strftime("%d/%m/%Y")
for example 28/11/2021 29/11/2021 no.of.days=1例如 28/11/2021 29/11/2021 no.of.days=1
If you have two datetime
objects, you can subtract them to get a timedelta
.如果您有两个
datetime
对象,则可以将它们相减以获得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用这个
You can read this documentation about comparing dates您可以阅读有关比较日期的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.