Objective-C 计算两个日期之间的天数
可能的重复:
如何比较两个日期并返回一个数字天数
我有两个日期(作为“yyyy-mm-dd”形式的 NSString),例如:
NSString *start = "2010-11-01";
NSString *end = "2010-12-01";
我想实现:
- (int)numberOfDaysBetween:(NSString *)startDate and:(NSString *)endDate {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
components
现在拥有差异。components
now holds the difference.有完整的日期和时间编程指南。这是相关部分 为您提供有关操作的提示。
这是另一个问题中的示例代码的来源。
尝试在此基础上写一些东西,然后如果您有具体问题再回来。
编辑
好的。以下是我如何以最基本的形式编写代码。
首先,我会扩展 NSDate。
头文件:
实现文件:
这是非常粗糙的代码。不会进行错误检查或验证第二个日期晚于第一个日期。
然后我会像这样使用它(在 64 位、垃圾收集环境中运行):
这太可惜了,因为通过发布代码和不正确的输出并获得更具体的帮助,您会学到更多东西。但如果你只是想成为一名剪切粘贴程序员;拿着这个代码,祝你好运。
There is a whole guide to Date and Time Programming. Here is a relevant section which gives you a hint about what to do.
It's where the example code comes from in the other question.
Try and write something based on that and then come back if you have specific questions.
Edit
Okay. Here is how I would write the code in it's most basic form.
First, I would extend NSDate.
The header file:
The implementation file:
This is very rough code. There is no error checking or validating that the second date is later than the first.
And then I would use it like this (running on a 64-bit, Garbage Collected environment):
This is such a shame, because you would have learned a lot more by posting your code and the incorrect outputs and getting more specific help. But if you just want to be a cut-and-paste programmer; take this code and good luck to you.
Swift 4 实现
方法调用:
方法实现:
Objective C 实现:
方法调用:
方法实现:
Swift 4 implementation
Method call :
Method Implementation:
Objective C implementation:
Method Call:
Method Implementation:
这段代码似乎在 Swift 2 中运行良好:
This code seems to work nicely in Swift 2:
ObjC 代码:
结果:
ObjC Code:
Result:
斯威夫特3:
Swift 3:
上面提到的例子只代表了解决方案的一半,因为大多数解决方案在计算差异时只考虑日期值(例如:2010-11-01),但在现实场景中 NSDate 实例总是与时间结合在一起并影响结果的差异。
现在,我们来谈谈公认的解决方案,其中用户定义了两个日期,但没有定义它们的时间。
这里两个日期的时间是相同的,因为没有为它们提供时间,这意味着它不会影响差异计算。
结果将是 days = 1
现在让我们为这两个日期添加特定时间:
添加特定时间后,结果会发生变化。现在days = 0即使日期中定义的日期不同。 这是因为两个日期之间的时差小于 24 小时。
只要满足以下条件,此计算方法就可以正常工作:
或者
但是,如果您想根据日历日期更改计算天数差异,那么就会出现问题,因为在上面的示例中,即使日历日期更改,但结果将是 天数 = 0。因为时差不到24小时。
那么,我们如何根据日历日期变化计算天数差异。以下是 Apple 建议的操作方法:
更多详细信息请参阅 这里
我在这个主题上的两分钱代表了图片的剩余一半。
The examples mentioned above represent only half picture of the solution because most of the solution only take date value (e.g: 2010-11-01) into account when calculating the difference however in-real world scenario NSDate instance always come together with time and impact the difference on the result as well.
Now, let's talk about the accepted solution where user defined the two dates but not time for them.
Here the time for both dates would be same because no time has been provided for them which means it doesn't impact on the difference calculation.
And the result would be days = 1
Now let's add a specific time for both dates:
After adding a specific time the result would change. Now days = 0 even though day defined in the dates are different. It's because the time difference between the two dates is less than 24 hours.
This calculation method is gonna work fine as long as:
Or
However, if you wanna calculate the days difference based on calendar date change then there would a problem because in the above example even though calendar date changed but the result would be days = 0. It's because of the time difference is less than 24 hours.
So, how we can calculate the days difference based on calendar date change. Here is the Apple suggested way of doing it:
More details are available here
My two cent on the topic to represent the remaining half of the picture.