您的代码似乎对我有用(我复制/粘贴它),我看不到您如何处理passedInDate
,我按照您指定的设置日期:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
//NSDate * dateA = [NSDate date];
NSDate * dateA = [formatter dateFromString:@"2009-12-15 06:09:00 +1100"];
NSDate * dateB = [formatter dateFromString:@"2009-12-15 04:17:00 +1100"];
NSUInteger currentMinuteOrdinal =
[[NSCalendar currentCalendar] ordinalityOfUnit:NSMinuteCalendarUnit
inUnit:NSEraCalendarUnit
forDate:dateA]; // current
NSUInteger passedInMinuteOrdinal =
[[NSCalendar currentCalendar] ordinalityOfUnit:NSMinuteCalendarUnit
inUnit:NSEraCalendarUnit
forDate:dateB]; // passed in
NSUInteger minuteDifference = currentMinuteOrdinal - passedInMinuteOrdinal;
NSLog(@" currentMinuteOrdinal: %d", currentMinuteOrdinal);
NSLog(@"passedInMinuteOrdinal: %d", passedInMinuteOrdinal);
NSLog(@" minuteDifference: %d", minuteDifference);
在这种情况下输出:
2009-12-15 21:51:46.215 x[50036:903] currentMinuteOrdinal: 1056606130
2009-12-15 21:51:46.216 x[50036:903] passedInMinuteOrdinal: 1056606018
2009-12-15 21:51:46.217 x[50036:903] minuteDifference: 112