我读过的所有问题,并回答所有关于这个主题的教程,但由于某些原因,它不是为我工作。总是告诉我这两个日期是同一日期!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;
}
}
非常感谢您!
为什么你从日期创建字符串然后回到日期? – rckoenes
因为我想要没有秒的日期,只有HH:mm。所以我使用短日期格式。 –
不需要使用日期格式化程序。请参阅下面的答案。 – Ilanchezhian