两个日期之间python中的整数差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16170737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:56:24  来源:igfitidea点击:

Integer difference in python between two dates

pythondatedatetime

提问by Todd Curry

I've RTFM and read many questions and answers here on SO regarding this, and was happily using strftime and strptime yesterday, so I would swear this should work, but it isn't....

我已经 RTFM 并在 SO 上阅读了许多关于此的问题和答案,并且昨天很高兴使用 strftime 和 strptime,所以我发誓这应该有效,但它不是......

I just want an integer. Not a "timedelta object." Not an "aware yet hashable object" (see, I RTFM). Not a tuple. Not a dictionary. Just a simple freaking integer so I can use an if statement and branch and be happy. Please bring the light of your wisdom upon this, with thanks.

我只想要一个整数。不是“timedelta 对象”。不是“已知但可散列的对象”(参见,我 RTFM)。不是元组。不是字典。只是一个简单的该死的整数,所以我可以使用 if 语句和分支并感到高兴。请带着你的智慧之光照亮这一切,感谢。

Here's what I have

这是我所拥有的

...
import datetime
mdate = "2010-10-05"
rdate = "2010-10-05"
mdate1 = datetime.strptime(mdate, "%Y-%m-%d")
rdate1 = datetime.strptime(rdate, "%Y-%m-%d")
delta =  datetime.timedelta.days(mdate1 - rdate1)

Here's what I get:

这是我得到的:

pmain.py:4: AttributeError: 'module' object has no attribute 'strptime'
(error hits in the 'mdate1..." line above)

And, that doesn't mean that my delta line is going to work -- please look at that one, too.

而且,这并不意味着我的 delta 线会起作用——也请看看那条线。

采纳答案by Martijn Pieters

You want to get the classmethoddatetime.datetime.strptime(), then take the .daysattributefrom the resulting timedelta:

您想要获取classmethoddatetime.datetime.strptime(),然后从结果 timedelta 中获取.days属性

import datetime

mdate = "2010-10-05"
rdate = "2010-10-05"
mdate1 = datetime.datetime.strptime(mdate, "%Y-%m-%d").date()
rdate1 = datetime.datetime.strptime(rdate, "%Y-%m-%d").date()
delta =  (mdate1 - rdate1).days

So you have the datetimemodule, which has a datetime.datetimeclass, which in turn has a datetime.datetime.strptime()method on it. I also added calls to .date()to extract justthe date portion (result is a datetime.dateinstance); this makes dealing with timestamps that differ slightly less than a multiple of 24 hours easier.

所以你有一个datetime模块,它有一个datetime.datetime类,而这个类又上有一个datetime.datetime.strptime()方法。我还添加了调用以.date()提取日期部分(结果是一个实例);这使得处理差异略小于 24 小时倍数的时间戳变得更容易。datetime.date

Demo:

演示:

>>> import datetime
>>> mdate = "2010-10-05"
>>> rdate = "2010-10-05"
>>> mdate1 = datetime.datetime.strptime(mdate, "%Y-%m-%d").date()
>>> rdate1 = datetime.datetime.strptime(rdate, "%Y-%m-%d").date()
>>> delta =  (mdate1 - rdate1).days
>>> print delta
0
>>> type(delta)
<type 'int'>

回答by shilpa.rpns

sign1['days'] = sign1['diff'] / np.timedelta64(1, 'D')

I had the same problem and it solved by uding the above statement. I hope it helps.

我遇到了同样的问题,它通过使用上述语句解决了。我希望它有帮助。