两个日期之间的区别?

编程入门 行业动态 更新时间:2024-10-25 06:28:12
本文介绍了两个日期之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用PYTHON编程...

Im programming with PYTHON...

我有两个不同的日期...我想知道他们之间的差异(以天为单位)。日期的格式是YYYY-MM-DD

I have two diferent dates...and I want to know the difference (in days) between them. The format of the date is YYYY-MM-DD

以更好的方式解释自己...

To explain myself in a better way...

我有一个功能,可以添加或SUBSTRACT给定的数字并获取日期....例如:

I have a function that can ADD or SUBSTRACT a given number and get the date....for example:

def addonDays(a,x): ret = time.strftime("%Y-%m-%d",time.localtime(time.mktime(time.strptime(a,"%Y-%m-%d"))+x*3600*24+3600)) return ret

其中A是日期和X我要添加的天数.....而结果是另一个日期....

where A is the date and X the number of days I want to add.....and the result is another date....

我需要一个我可以给出两个日期,结果将是一个int与差异的日子....

I need a function where I can give two dates and the result would be an int with the days of difference....

我希望有人可以帮助! THANKS

I hope someone could help!! THANKS

推荐答案

使用 - 两个 datetime 对象,并采取天成员。

Use - to get the difference between two datetime objects and take the days member.

from datetime import datetime def days_between(d1, d2): d1 = datetime.strptime(d1, "%Y-%m-%d") d2 = datetime.strptime(d2, "%Y-%m-%d") return abs((d2 - d1).days)

更多推荐

两个日期之间的区别?

本文发布于:2023-10-18 23:51:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   两个   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!