2013-12-07 3 views
0

간단히 말해서 현재 dateTime을 가져 와서 시간을 변경하고 +0800을 시간대에 적용하여 말레이시아 시간으로 변경합니다.NSString을 NSDate로 변환 변환 - 잘못된 결과

결과는 예기치 않은 :

-(NSDate *)departureDateTime 
{ 
    NSDate *date = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; 
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date]; 
    [components setHour: 7]; 
    [components setMinute: 59]; 
    [components setSecond: 17]; 

    NSDate *newDate = [gregorian dateFromComponents: components]; 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyy-mm-dd HH:mm:ss Z"]; 
    [dateFormat setLocale:[NSLocale currentLocale]]; 


    NSString *newDateString = [NSString stringWithFormat:@"%@",newDate]; 
    NSString *maskString = [NSString stringWithFormat:@"%@", [newDateString substringToIndex:20]]; 
    NSString *append = [maskString stringByAppendingString:@"+0800"]; 
    NSLog(@"%@",append); 


    NSDate *finalLocalDate = [dateFormat dateFromString:append]; 

    return finalLocalDate; 
} 

결과 : NSLog 추가]에 대한

: 2013-12-07 23:59:17 +0800

하지만 finalLocalDate : 2013-01-07 15:59:17 +0000

+0

모든 시간대를 현지 시간대로 설정하지 않으므로 모든 날짜 변환은 현지 시간대로 표시됩니다. 그리고 NSDate는 항상 GMT로 인쇄합니다. –

+0

BTW, 월 형식을 지정하려면 "mm"이 아니라 "MM"을 사용하십시오. –

답변

1

훨씬 짧은 해결책으로 답을 찾았으니, 앞으로 누군가를 돕기 위해 여기에 올렸습니다.

-(NSDate *)departureDateTime 
{ 
    NSDate *date = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; 
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date]; 

    [components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ]; 
    [components setHour: 7]; 
    [components setMinute: 59]; 
    [components setSecond: 17]; 

    NSDate *newDate = [gregorian dateFromComponents: components]; 
    NSLog(@"%@",newDate); 

    return newDate; 
} 

정확한 결과 우리가 시스템 시간대의 시간대를 설정

[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ]; 

의 라인을 추가하여되도록

복귀 문제는 우리가 불필요한 코드를 제거 다른 시간대이었다 2013-12-08 07:59:17 +0000

+0

정확히 무엇을하고 있는지는 분명하지 않지만 NSDate가 현지 시간으로 인쇄하고 있다면 잘못 처리 한 것입니다. NSDate는 GMT를 포함해야합니다. –

+0

실제로, 당신은 GMT -'(+ 0 * 3600)'을 0으로 설정하고있는 것으로 보입니다. 따라서 당신은 어떤 시간대 보정도 얻지 못하고 있습니다. (이것은 거의 확실하게 당신이 의도 한 것이 아닙니다.) –

+0

@HotLicks NSDate의 결과를 얻을 때마다 - 내 시스템 시간 (말레이시아)과 다른 8 시간. 그 이유는 무엇입니까? – Ben

0

이 시도 :

NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; 
NSDate *malaysianTime = [NSDate dateWithTimeIntervalSince1970:now+(8*60*60)]; //8h in seconds 
+0

고맙습니다. 해 보았습니다. 솔루션도 사용할 수있었습니다. – Ben

0

컴퓨터가 올바른 시간대로 실행중인 경우 시간대를 설정하지 마십시오. 원하는 시간대 컴퓨터/전화가 현재 사용중인 아닌 경우

NSDateFormatter* fmt = [[NSDateFormatter alloc] init]; 
[fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 
NSString* printableDate = [fmt stringFromDate:newDate]; 

NSLog(@"The date is %@", printableDate); 

만 (NSCalendar와 NSDateFormatter 모두) 시간대를 설정 -

은 다음 newDate 값을 만듭니다.

NSLog newDate의 경우 직접 GMT 시간대로 인쇄됩니다. 이것은 그것이 있어야하는 방법입니다 - 당신은 항상 NSDateFormatter를 인쇄 가능한 날짜로 사용합니다. NSDate를 NSLog로 직접 작성하면 설계 상 GMT를 얻게됩니다.