0

2 つの日付の違い (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 に答える