2011-08-10 6 views
0

NSDate *newDateの値は2011-08-12 13:00:00 +0000です。 今、私は次のようにたDateFormatterによって文字列に変換しようとすると:私はdatestrを得るためにiphoneの日付形式に関する問題

NSDateFormatter *dateFormatter= [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss Z"]; 

NSString *datestr = [[dateFormatter stringFromDate:newDate] lowercaseString];  

しかし、値がnewDate値より5時30時間以上です。私はどこが間違っているのか分かりません。 datestrの値が2011-08-12 18:30:00 + 5:30(これは間違っています)と表示されます。

+1

あなたは本当にStackOverflowでフォーマットする方法を知っておくべき77の質問をした後、是非、来て!あなたの質問にもっと努力してください、人はひどくフォーマットされた質問を読むことさえしません。 – DarkDust

答えて

2

基本的に時刻を現​​地時間帯に変換しています。

[dateFormatter setTimeZone:[NSTimeZone <use any of the functions below to get your desired one>]]; 

ドキュメントのリンク:あなたが欲しいだけ設定することを日付フォーマッタのための時間AONEいけない場合 http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/

Creating and Initializing Time Zone Objects 
+ timeZoneWithAbbreviation: 
+ timeZoneWithName: 
+ timeZoneWithName:data: 
+ timeZoneForSecondsFromGMT: 
– initWithName: 
– initWithName:data: 
+ timeZoneDataVersion 
Working with System Time Zones 
+ localTimeZone 
+ defaultTimeZone 
+ setDefaultTimeZone: 
+ resetSystemTimeZone 
+ systemTimeZone 
0

を、それがタイムゾーン/ UTCのものでなければならないように見えます。日付はおそらくグローバル/世界標準時に返されており、必要なものは現地時間です。

IOSの文書によると:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html - あなたはあなたが望む時間帯を指定するためにロケールを含めることができます。

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800]; 

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
[dateFormatter setLocale:usLocale]; 

NSLog(@"Date for locale %@: %@", 
    [[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);