-1

I'm using this code to compare two dates and return the number of difference in days. I want to use it and get the minutes and the Hours, but I get the same number.

NSDate *todaysDate =[NSDate date];
NSDate *eventDate = [self.event objectForKey:@"eventDate"];

NSDateComponents *components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit
                                                                   fromDate: todaysDate toDate: eventDate options: 0];

NSInteger days = [components day];
NSInteger hours = [components hour];

NSLog(@"Number of days %ld", days);
NSLog(@"Number of hours %ld", hours);

the eventDate its on this format "2014-08-08 13:11:00 +0000"

What I always get in return for the number of hour or minutes is "9223372036854775807".

user3241911
  • 481
  • 6
  • 15

1 Answers1

0

If you enter "9223372036854775807" as a decimal value into a programming calculator and convert it to hex, you get 0x7FFFFFFFFFFFFFFF. That is probably the NSUndefinedDateComponent constant, and that means the NSDateComponents object doesn't have the requested fields because you didn't ask for them using NSHourCalendarUnit and NSMinuteCalendarUnit when you created the NSDateComponent. Only those fields you specify in the "components:" parameter will be initialized. The days field should contain a correct value. To get hours and minutes, you need to change this line:

NSDateComponents *components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit
                                          | NSHourCalendarUnit | NSMinuteCalendarUnit
                                          fromDate: todaysDate toDate: eventDate options: 

0];

Dko
  • 820
  • 6
  • 12