2012-10-23 35 views
1

我读过的所有问题,并回答所有关于这个主题的教程,但由于某些原因,它不是为我工作。总是告诉我这两个日期是同一日期!NSDate的比较都不尽如人意

请有人帮我指点迷津,我只是想检查是否一个比另一个大(包括日期和时间 - 无需秒),或者如果他们是平等的。

这是我的代码:

- (BOOL)isEndDateIsBiggerThanCurrectDate:(NSDate *)checkEndDate 
{ 
    NSString *endd = [NSDateFormatter localizedStringFromDate:checkEndDate 
                 dateStyle:NSDateFormatterShortStyle 
                 timeStyle:NSDateFormatterShortStyle]; 

    NSString *curreeeent = [NSDateFormatter localizedStringFromDate:[NSDate date] 
                 dateStyle:NSDateFormatterShortStyle 
                 timeStyle:NSDateFormatterShortStyle]; 

    NSDateFormatter * df = [[NSDateFormatter alloc]init];; 

    NSDate * newCurrent = [df dateFromString:endd]; 
    NSDate * newEnd = [df dateFromString:curreeeent]; 

    switch ([newCurrent compare:newEnd]) 
    { 
     case NSOrderedAscending: 
      return YES; 
      break; 
     case NSOrderedSame: 
      return NO; 
      break; 
     case NSOrderedDescending: 
      return NO; 
      break; 
    } 
} 

非常感谢您!

+0

为什么你从日期创建字符串然后回到日期? – rckoenes

+0

因为我想要没有秒的日期,只有HH:mm。所以我使用短日期格式。 –

+0

不需要使用日期格式化程序。请参阅下面的答案。 – Ilanchezhian

回答

0

我已经得到了答案,只是检查两个日期和比较它的确切时间。

- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate 
{ 
    NSDate* enddate = checkEndDate; 
    NSDate* currentdate = [NSDate date]; 
    NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate]; 
    double secondsInMinute = 60; 
    NSInteger secondsBetweenDates = distanceBetweenDates/secondsInMinute; 

    if (secondsBetweenDates == 0) 
     return YES; 
    else if (secondsBetweenDates < 0) 
     return YES; 
    else 
     return NO; 
} 
-2

你为什么不更改日期,时间间隔自1970年以来和排序通过。极其简单的数字比较,更快不是字符串进行比较,他们将永远排序正确的,不喜欢1,10,11,2,21,22,3,....

NSDate *now = [NSDate date]; 
NSTimeInterval ti = [now timeIntervalSince1970]; 

完蛋了。没有新的对象创建,在CPU上更快,更少的征税。

这里看你如何摆脱秒,但它很容易,因为你有一个数字,几秒钟。看到这里How to set seconds to zero for NSDate

+1

'NSTimeInterval'是**不是**整数,而是**。 – JustSid

+0

NSTimeInterval会用秒和毫秒,而这个问题是关于贝因在同一分钟 – Mark

+0

等于取决于你如何创建日期,秒和毫秒可以为零。如果您为前例创建日期。 23-12-2012 12:00,你没有多余的秒或毫秒 –

0

你确实应该使用的时间间隔,而不是日期和字符串之间的转换。

类似下面应该满足您的需求:

//current time 
NSDate *now = [NSDate date]; 

//time in the future 
NSDate *distantFuture = [NSDate distantFuture]; 

//gather time interval 
if([now timeIntervalSinceDate:distantFuture] > 0) 
{ 
    //huzzah! 
} 
+0

如果我有日期23/10/12 11:32我希望它与当前日期进行比较,是timeIntervalSinceDate这样做?没有秒?只有几小时几分钟? –

+0

timeIntervalSinceDate也会考虑秒。 – CaptainRedmuff

+0

将日期中的秒数设置为0(零) –

1

为此,您必须使用NSCalender

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSInteger desiredComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit); 


NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:[NSDate date]]; 
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate: checkEndDate]; 

NSDate *first = [calendar dateFromComponents:firstComponents]; 
NSDate *second = [calendar dateFromComponents:secondComponents]; 

NSComparisonResult result = [first compare:second]; 
if (result == NSOrderedAscending) { 
    //checkEndDate is before now 
} else if (result == NSOrderedDescending) { 
    //checkEndDate is after now 
} else { 
    //both are same 
}