0
votes

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

4 Answers

4
votes

Try this,

 - (NSInteger)timeDifference:(NSDate *)fromDate ToDate:(NSDate *)toDate{

    HMLogger_Info(@"<INPUT PARAMETERS> FromDate: %@, ToDate: %@",fromDate,toDate);

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *differenceValue = [calendar components:NSHourCalendarUnit
                                                    fromDate:fromDate toDate:toDate options:0];
    HMLogger_Info(@"Calculated hour difference : %d",[differenceValue hour]);
    NSInteger hourDifference = [differenceValue hour];

    HMLogger_Info(@"<RETURN VALUE>: HourDifference: %d",hourDifference);
    return hourDifference;
}
0
votes
Try this:   

 NSDate *dt2 = [[NSDate alloc]init];
    NSString *dateStr=@"10/25/2013 11:22:33";
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    dt2=[dateFormatter dateFromString:dateStr];
    NSDate *dt1=[NSDate date];
    NSUInteger unitFlags = NSDayCalendarUnit;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:dt1 toDate:dt2 options:0];
    int i=[components day]+1;
    int j=[components hour];
    int k=[components minute];
    NSLog(@"Days diff=%d, =%d, =%d",i,j,k);
0
votes

I think it helps to you

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit|NSHourCalendarUnit fromDate:fromDate toDate:toDate options:0];
NSLog(@"The difference between from date and to date is %d days and %d hours",components.day,components.hour);
0
votes
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *firstDate = @"2014-06-05 12:55:00";

NSDate *date=[dateFormatter dateFromString:firstDate];

NSDate *currentDate = [NSDate date];

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:date toDate:currentDate options:0];

NSLog(@"The difference between from date and to date is %d days and %d hours and %d minute and %d second",components.day,components.hour,components.minute,components.second);

Output: DateBookApp[1179:70b] The difference between from date and to date is 0 days and 22 hours and 57 minute and 12 Second