2017-07-04 24 views
0
NSString *[email protected]"2017-07-06T10:00:00"; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-ddTHH:mm:ss"; 

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
[dateFormatter setTimeZone:destinationTimeZone]; 

NSDate *oldTime = [dateFormatter dateFromString:strUTCTime]; 

NSString *estDateString = [dateFormatter stringFromDate:oldTime]; 
NSLog(@"Local time is ===> %@",estDateString); 
+0

あなた自身をデバッグするか、少なくとも有用な結果を表示する必要があります。例えば ​​'oldTime'は' nil'です。そのため、 'estDateString'はとにかくありません。ですから、あなたの問題は 'oldTime'を得ることからです。実際、 'timeDone'を削除してNSDateを取得できるかどうかを確認しました。それは問題が 'dateFormat'であることを知るのを助けます、' T' => 'dateFormatter.dateFormat = @" yyyy-MM-dd'T'HH: ":esc"; " – Larme

+0

はい、Tのおかげでエスケープする必要があります。 =====> dateFormatter.dateFormat = @ "yyyy-MM-dd'T'HH:mm:ss"; – user2964955

答えて

1

次の作業およびテスト済みのコードを示します。

NSString *[email protected]"2017-07-06T10:00:00"; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss"; 
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en-US"]]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 

NSDate *oldTime = [dateFormatter dateFromString:strUTCTime]; 

NSLog(@"UTC time is ===> %@",oldTime); 

NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone]; 
[dateFormatter setTimeZone:destinationTimeZone]; 
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 

NSString *estDateString = [dateFormatter stringFromDate:oldTime]; 
NSLog(@"Local time is ===> %@",estDateString); 

どうすればいいのか教えてください。

+0

感謝は魅力のように機能します。 – user2964955

+0

それは間違いなくupvoteを動かしています。 –

関連する問題