-2

現在のデバイスの時刻は --- 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

4

5 に答える 5

1

あなたのtimestamp文字列はGMTです。これは、GMT 文字列を に戻すNSDate場合、日付フォーマッタをローカル タイムゾーンではなく GMT に設定する必要があることを意味します。これを行うことで、適切なNSDate.

NSDateまた、オブジェクトをログに記録すると、他に関係なく GMT で表示されることを忘れないでください。

于 2013-05-10T05:10:30.017 に答える
0

グーグルで少し努力してください。最初にグーグルで質問を検索し、その場合に答えが得られない場合は、質問をスタックオーバーフローに置いてください。そして、以下のリンクであなたの答えを見てください....

  1. iPhone: NSDate は GMT を現地時間に変換します
  2. 現地時間へのiphone時間変換GMT
  3. ユーザーの現地時間への日付/時刻の変換 - 問題
于 2013-05-10T05:09:17.507 に答える
0

// 現在の日付/時刻を取得

NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
[dateFormatter release];
NSLog(@"User's current time in their preference format:%@",currentTime);
于 2013-05-10T05:07:48.640 に答える
0
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);



    NSTimeZone *localTime = [NSTimeZone systemTimeZone];
    NSLog(@"local timezone  is------ %@",[localTime name]);


    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:sourceTimeZone];
    NSDate *dateFromString = [dateFormatter dateFromString:timeStamp];
    NSLog(@"local time is ------- %@",dateFromString);


    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    dateFormatter1.dateFormat = @"yyyy-MM-dd HH:mm";

    NSTimeZone *gmt1 = [NSTimeZone localTimeZone];
    [dateFormatter1 setTimeZone:gmt1];
    NSString *timeStamp1 = [dateFormatter1 stringFromDate:[NSDate date]];
    NSLog(@"local time is ------- %@",timeStamp1);

出力:

time in GMT ------ 2013-05-10 05:13 AM
 local timezone  is------ Asia/Kolkata
 local time is ------- 2013-05-10 00:13:00 +0000
 local time is ------- 2013-05-10 10:43
于 2013-05-10T05:13:14.180 に答える