views:

94

answers:

1

I have the following code in my program:

NSUInteger currentMinuteOrdinal = [[NSCalendar currentCalendar] ordinalityOfUnit:NSMinuteCalendarUnit inUnit:NSEraCalendarUnit forDate:[NSDate date]];
NSUInteger passedInMinuteOrdinal = [[NSCalendar currentCalendar] ordinalityOfUnit:NSMinuteCalendarUnit inUnit:NSEraCalendarUnit forDate:passedInDate];
NSUInteger minuteDifference = currentMinuteOrdinal - passedInMinuteOrdinal;

The current time is today at 6:09pm. The passed-in time is today at 4:17pm. (I'm looking at the values in the debugger so I know these values are correct.) Yet these two ordinal values always come out to the same value, rendering the 'minuteDifference' as 0.

Does this method simply not work? Or am I doing something horribly wrong?

Thanks.

A: 

Your code seems to work for me (I copy/paste it), I can't see how you handle passedInDate, I setup dates as you specified:

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);

Output in this case:

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
stefanB