两个日期之间的区别? [英] Difference between two dates?

查看:186
本文介绍了两个日期之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用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)

这篇关于两个日期之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆