ios 计算Objective-C中两个日期之间的天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4575689/
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
Calculating the number of days between two dates in Objective-C
提问by CodeGuy
Possible Duplicate:
How can I compare two dates, return a number of days
可能的重复:
如何比较两个日期,返回天数
I have two dates (as NSString in the form "yyyy-mm-dd"), for example:
我有两个日期(作为“yyyy-mm-dd”形式的 NSString),例如:
NSString *start = "2010-11-01";
NSString *end = "2010-12-01";
I'd like to implement:
我想实现:
- (int)numberOfDaysBetween:(NSString *)startDate and:(NSString *)endDate {
}
回答by vikingosegundo
NSString *start = @"2010-09-01";
NSString *end = @"2010-12-01";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
fromDate:startDate
toDate:endDate
options:0];
components
now holds the difference.
components
现在保持差异。
NSLog(@"%ld", [components day]);
回答by Abizern
There is a whole guide to Date and Time Programming. Here is a relevant sectionwhich 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.
首先,我会扩展 NSDate。
The header file:
头文件:
// NSDate+ADNExtensions.h
#import <Cocoa/Cocoa.h>
@interface NSDate (ADNExtensions)
- (NSInteger)numberOfDaysUntil:(NSDate *)aDate;
@end
The implementation file:
实现文件:
// NSDate+ADNExtensions.m
#import "NSDate+ADNExtensions.h"
@implementation NSDate (ADNExtensions)
- (NSInteger)numberOfDaysUntil:(NSDate *)aDate {
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self toDate:aDate options:0];
return [components day];
}
@end
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):
然后我会像这样使用它(在 64 位垃圾收集环境中运行):
NSDate *startDate = [NSDate dateWithString:@"2010-11-01 00:00:00 +0000"];
NSDate *endDate = [NSDate dateWithString:@"2010-11-02 00:00:00 +0000"];
NSInteger difference = [startDate numberOfDaysUntil:endDate];
NSLog(@"Diff = %ld", difference);
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.
这真是太可惜了,因为通过发布代码和不正确的输出并获得更具体的帮助,您会学到更多。但如果你只是想成为一个剪切和粘贴的程序员;拿着这个代码,祝你好运。
回答by Naishta
Swift 4 implementation
Swift 4 实现
Method call :
方法调用:
let numberOfDays = daysBetweenDates(startDate: fileCreatedDate,endDate: date)
Method Implementation:
方法实现:
func daysBetweenDates(startDate: Date, endDate: Date) -> Int {
let daysBetween = Calendar.current.dateComponents([.day], from: startDate, to: endDate)
print(daysBetween.day!)
return daysBetween.day!
}
Objective C implementation:
目标 C 实现:
Method Call:
方法调用:
int numberOfDaysSinceFileCreation = [self daysBetweenDates: fileCreatedDate
currentDate: today];
Method Implementation:
方法实现:
- (int) daysBetweenDates: (NSDate *)startDate currentDate: (NSDate *)endDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponent = [calendar components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:0];
int totalDays = (int)dateComponent.day;
return totalDays;
}
回答by iphaaw
This code seems to work nicely in Swift 2:
这段代码在 Swift 2 中似乎运行良好:
func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int
{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
return components.day
}
回答by Gobi M
ObjC Code:
对象代码:
NSDateComponents *dateComponent = [calender components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:0];
Result:
结果:
int totalDays = (int)dateComponent.day;
回答by Blixt
Swift 3:
斯威夫特 3:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let start = formatter.date(from: "2010-09-01")!
let end = formatter.date(from: "2010-12-01")!
let days = Calendar.current.dateComponents([.day], from: start, to: end).day!