** Solved. Code updated to work correctly **
I'm trying to calculate the difference in days, hours, minutes, and seconds from the current date/time and a future date/time.
What have I done wrong? First of all it doesn't look like the timezone is working properly. Second of all the number of hours, minutes, and seconds are way off.
Result: Current date: 2013-09-12 05:42:24 +0000 (should be 10:43pm PST) Future Date: 2013-09-29 23:00:00 +0000 (should be 4pm PST)
Days: 17 Hours: 596523.235278 (should be ~425) Minutes: 35791394 (should be ~25516) Seconds: 2147483647 (should be ~1530960)
// Get today's Date
NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *todaysComps = [gregorian1 components: NSUIntegerMax fromDate: todaysDate];
[todaysComps setTimeZone:[NSTimeZone timeZoneWithName:@"America/Vancouver"]];
NSDate *todayFinalDate = [gregorian1 dateFromComponents: todaysComps];
NSLog(@"Current date: %@", todayFinalDate);
// Set Future Date
NSDate *futureDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: futureDate];
[todaysComps setTimeZone:[NSTimeZone timeZoneWithName:@"America/Vancouver"]];
[components setHour: 16];
[components setMinute: 00];
[components setSecond: 00];
[components setDay: 29];
[components setMonth: 9];
[components setYear: 2013];
NSDate *newDate = [gregorian dateFromComponents: components];
NSLog(@"Future Date: %@", newDate);
// Check time between the two NSDateComponents *difference = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:todaysDate toDate:newDate options:0]; int dayDiff = [difference day];
difference = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int hourDiff = [difference hour];
difference = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int minDiff = [difference minute];
difference = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int secDiff = [difference second];
self.daysLabel.text=[NSString stringWithFormat:@"%d",dayDiff];
self.hoursLabel.text=[NSString stringWithFormat:@"%d",hourDiff];
self.minutesLabel.text=[NSString stringWithFormat:@"%d",minDiff];
self.secondsLabel.text=[NSString stringWithFormat:@"%d",secDiff];
NSLog(@"\nDays: %i\nHours: %d\nMinutes: %i\nSeconds: %i",dayDiff,hourDiff,minDiff,secDiff);