現在のデバイスの時刻は --- 2013-05-09 10:23 AM
次のコードでint GMTに変換します
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"time in GMT ------ %@",timeStamp);
そして、私は出力を得ました
time in GMT ------ 2013-05-10 04:53 AM
それから私は自分のタイムゾーンを見つけました
NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(@"local timezone is------ %@",[localTime name]);
アウトプット
local timezone is------ Asia/Kolkata
ここで、上記の GMT 時間を現地時間に変換する必要があります。次のコードを使用しました
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:[localTime name]];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
NSLog(@"local time is ------- %@",dateFromString);
しかし、私は間違った出力を得ています..
local time is ------- 2013-05-09 19:23:00 +0000
なぜこれが起こるのですか?現地時間を取得する必要があります - 2013-05-09 10:23 AM