0

两个日期之间的差异怎么可能是错误的 (2013-04-04T19:14:58+02:00-2013-04-03T18:14:58+02:00) ?

代码的结果是

01 天 06 小时 46 分 02 秒

结果完全错误正确的答案应该是

01 天 01 小时 00 分钟 00 秒

你能帮我找出问题的根源吗?

编码 :

-(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 函数:

+(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;
}
4

2 回答 2

4

我无法理解你的逻辑,但如果你想以你想要NSDateComponents的方式获得差异,你可以使用:

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

这个产量结果:

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

所以……又怎么了?

于 2013-04-03T16:50:10.223 回答
1

要获得两个 NSDate 对象之间的差异,请使用timeIntervalSinceDate:. 然后,要计算天、小时、分钟和秒之间的时间,请使用模除法。

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;

您可以将 NSTimeInterval (double) 转换为 long 不同的方式,具体取决于您是要向上舍入、向下舍入还是最接近。

于 2013-04-03T17:16:15.430 回答