Ios 测试NSDate比较的最佳方法是什么?

Ios 测试NSDate比较的最佳方法是什么?,ios,objective-c,cocoa-touch,nsdate,Ios,Objective C,Cocoa Touch,Nsdate,我有两个选择约会的人,他们给了我开始时间和结束时间。根据这一准则: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.timeZone=[NSTimeZone defaultTimeZone]; dateFormatter.timeStyle=NSDateFormatterShortStyle; dateFormatter.dateStyle=NSDateFormatterShortStyl

我有两个选择约会的人,他们给了我开始时间和结束时间。根据这一准则:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);
在my.h中

@property (weak, nonatomic) IBOutlet UIDatePicker *startTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime;
我有一段代码,可以比较给定时间和当前时间。什么是最好的方式来测试它,因为我需要你在后台使用它

if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime"); 
}

if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime"); 
}
我需要在那个代码和这个代码之间进行选择。那么
NSLog
在这里工作正常吗

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0)
{
//start time greater than today
}

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0)
{
//start time less than today
}

else
{
//both dates are equal
}
-(NSTimeInterval)timeIntervalSinceDate:(NSDate*)另一个日期
如果([startTime.date timeIntervalSinceDate:[NSDate date]]]>0)
{
//开始时间大于今天
}
否则如果([startTime.date TimeIntervalsIncestate:[NSDate date]]]<0)
{
//开始时间比今天短
}
其他的
{
//两个日期相同
}

谢谢

您可以使用NSComparator比较两个对象的值

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *strDate;
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
strDate = [formatter dateFromString:@"2012-12-07 08:43:31"];
endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];

NSLog(@"%d",[strDate compare:endDate]);
if([strDate compare:endDate] < 0)
    NSLog(@"Enddate bigger than serdate");
else if([strDate compare:endDate] > 0)
    NSLog(@"strdate bigger than enddate");
else
    NSLog(@"Both Are Identicle");
NSDateFormatter*formatter=[[NSDateFormatter alloc]init];
NSDate*标准日期;
NSDate*endDate;
[格式化程序setDateFormat:@“yyyy-MM-dd HH:MM:ss”];
标准日期=[formatter dateFromString:@“2012-12-07 08:43:31”];
endDate=[formatter dateFromString:@“2012-12-07 08:43:30”];
NSLog(@“%d”,[strDate compare:endDate]);
如果([strDate比较:endDate]<0)
NSLog(@“Enddate大于serdate”);
else if([strDate compare:endDate]>0)
NSLog(@“标准日期大于结束日期”);
其他的
NSLog(“两者都是相同的”);

您可以使用NSComparator比较两个对象的值

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *strDate;
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
strDate = [formatter dateFromString:@"2012-12-07 08:43:31"];
endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];

NSLog(@"%d",[strDate compare:endDate]);
if([strDate compare:endDate] < 0)
    NSLog(@"Enddate bigger than serdate");
else if([strDate compare:endDate] > 0)
    NSLog(@"strdate bigger than enddate");
else
    NSLog(@"Both Are Identicle");
NSDateFormatter*formatter=[[NSDateFormatter alloc]init];
NSDate*标准日期;
NSDate*endDate;
[格式化程序setDateFormat:@“yyyy-MM-dd HH:MM:ss”];
标准日期=[formatter dateFromString:@“2012-12-07 08:43:31”];
endDate=[formatter dateFromString:@“2012-12-07 08:43:30”];
NSLog(@“%d”,[strDate compare:endDate]);
如果([strDate比较:endDate]<0)
NSLog(@“Enddate大于serdate”);
else if([strDate compare:endDate]>0)
NSLog(@“标准日期大于结束日期”);
其他的
NSLog(“两者都是相同的”);
然后,通过以下比较,可以看出哪个较早/较晚/相同:

 if ([date1 compare:date2] == NSOrderedDescending) {
 NSLog(@"date1 is later than date2");        

 } else if ([date1 compare:date2] == NSOrderedAscending) {
  NSLog(@"date1 is earlier than date2");

 } else {
NSLog(@"dates are the same");
 }
然后,通过以下比较,可以看出哪个较早/较晚/相同:

 if ([date1 compare:date2] == NSOrderedDescending) {
 NSLog(@"date1 is later than date2");        

 } else if ([date1 compare:date2] == NSOrderedAscending) {
  NSLog(@"date1 is earlier than date2");

 } else {
NSLog(@"dates are the same");
 }


看看这里:这有点相关,但我并没有真正理解它!我只是想知道打印NSLog是否是此类功能的理想测试,或者实际上有更好的方法来处理我的请求?其他问题:wy NSLog在那里不起作用吗?@vikingosegundo,因为我需要在后台使用它,当我使用NSLog测试时,它不会返回任何结果!我开始认为,我在ViewController中声明NSDate并在AppDelegate中调用它的方式就是问题所在!你知道吗?如果nslog什么也不打印,那可能是2个原因:1)语句从未到达2)打印的对象不存在。是零。请看这里:它有点相关,但我并没有真正理解它!我只是想知道打印NSLog是否是此类功能的理想测试,或者实际上有更好的方法来处理我的请求?其他问题:wy NSLog在那里不起作用吗?@vikingosegundo,因为我需要在后台使用它,当我使用NSLog测试时,它不会返回任何结果!我开始认为,我在ViewController中声明NSDate并在AppDelegate中调用它的方式就是问题所在!你知道吗?如果nslog什么也不打印,那可能是2个原因:1)语句从未到达2)打印的对象不存在。是零。你对如何使变量全局化有什么想法吗?所以我可以把它从ViewController带到appdelgate中的方法didEnterBackground?使用SharedManager在AppDelegate中创建变量。你可以在任何地方使用它们。我喜欢这个主意!!但是我怎么用呢?请给我一个如何声明它的示例代码,你会得到正确的答案。我想这就是我的问题所在。关于取消排序的问题,我已经有了完全相同的NSDate,它触发了“取消排序”,我不知道为什么?你对如何使变量全局化有想法吗?所以我可以把它从ViewController带到appdelgate中的方法didEnterBackground?使用SharedManager在AppDelegate中创建变量。你可以在任何地方使用它们。我喜欢这个主意!!但是我怎么用呢?请给我一个如何声明它的示例代码,你会得到正确的答案。我想这就是我的问题所在。关于取消排序的问题,我已经有了完全相同的NSDate,它触发了“取消排序”,我不知道为什么?你对如何使变量全局化有想法吗?所以我可以把它从ViewController带到appdelgate中的didEnterBackground方法?你对如何使变量全局化有什么想法吗?所以我可以把它从ViewController转到appdelgate中的didEnterBackground方法?