0

How is possible that the difference between two dates is wrong (2013-04-04T19:14:58+02:00-2013-04-03T18:14:58+02:00) ?

The result of the code is

01 days 06 hours 46minutes 02 seconds

the result is totally wrong the right answer should be

01 days 01 hours 00 minutes 00 seconds

Can you pls help me to figure out the source of the issue ?

The Code :

-(void)startTimer {
    timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];

}




-(void) updateCountdown
{

    NSDate *expirydate = [MSharedFunctions SqldateStringTodate:@"2013-04-04T19:14:58+02:00"];

    NSDate *now = [MSharedFunctions SqldateStringTodate:@"2013-04-03T18:14:58+02:00"];

    if ([expirydate isEarlierThanDate:[MSharedFunctions SqldateStringTodate:[MSharedFunctions SqldateStringTodateNow]]]) {
        //Fire end counter

        //[[NSNotificationCenter defaultCenter] postNotificationName:@"TIMER" object:nil];
    }
    //NSDate *dateFromString = [[NSDate alloc] init];
    NSDate *dateFromString =expirydate;



    //NSLog(@"now %@:  endtime :%@ ",[MSharedFunctions SqldateStringTodateNow],@"2013-04-03T18:14:58+02:00");
    NSCalendar *calendar =  [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
    NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];
    NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];
    NSDateComponents *componentsDaysDiff = [calendar components:NSDayCalendarUnit
                                                                fromDate:now
                                                                  toDate:dateFromString
                                                                 options:0];



            NSLog(@"%@",[NSString stringWithFormat:@"%02d",componentsDaysDiff.day]);
            NSLog(@"%@",[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)]);
            NSLog(@"%@",[NSString stringWithFormat:@"%02d",(60-componentMint.minute)]);
            NSLog(@"%@",[NSString stringWithFormat:@"%02d",(60-componentSec.second)]);




}

SqldateStringTodate Function :

+(NSDate*)SqldateStringTodate:(NSString*) stringdate {
    stringdate = [stringdate stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange([stringdate length] - 5,5)];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
    NSLocale* formatterLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] ;
    [dateFormatter setLocale:formatterLocale];
    if (stringdate.length==24) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    }
    else {
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    }

    //DebugLog(@"New Date %@",stringdate);
    NSDate *getDate = [dateFormatter dateFromString:stringdate];
    return getDate;
}
molwiko
  • 323
  • 1
  • 6
  • 14
  • You're problem is that you're computing your hours, minutes, and seconds from "now", rather than fromDate/toDate. – Hot Licks Apr 03 '13 at 17:22

2 Answers2

4

I could not understand your logic, but if you want to get the difference as NSDateComponents in the way you want, you can use:

NSUInteger unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *componentsDaysDiff = [calendar components:unitFlags
                                                   fromDate:now
                                                     toDate:dateFromString
                                                    options:0];
NSLog("%@", componentsDayDiff);

This yield results:

<NSDateComponents: 0x75939f0>
Leap month: no
Day: 1
Hour: 1
Minute: 0
Second: 0

So... What's wrong with it again?

tia
  • 9,518
  • 1
  • 30
  • 44
  • This is definitely the way to do it. The advantage of this approach is you will be able to get the time difference easily by units that otherwise require much more complex calendar based calculations. – svena Apr 03 '13 at 20:09
  • there is no issue with it Thanks, it work – molwiko Apr 03 '13 at 20:36
1

To get the difference between two NSDate objects use timeIntervalSinceDate:. Then, to compute days, hours, minutes, and seconds between, use modulo division.

NSTimeInterval doubleDiff = [date1 timeIntervalSinceDate:date2];
long diff = (long) doubleDiff;
int seconds = diff % 60;
diff = diff / 60;
int minutes = diff % 60;
diff = diff / 60;
int hours = diff % 24;
int days = diff / 24;

You can fudge the conversion from NSTimeInterval (double) to long different ways, depending on whether you want to round up, down, or to closest.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Yes I know this solution but I would like to use the calendar components function – molwiko Apr 03 '13 at 20:23
  • @Molwiko -- But you see where you were going wrong? You were computing hours/minutes/seconds with fromDate but no toDate. Then doing some odd arithmetic when you printed out the results. – Hot Licks Apr 03 '13 at 20:47