2017-06-14 9 views
0

を示すミリ秒とNSDateFormatter現在の日付、私は次の形式で現在の日付を生成するNSDateを実装しようとしています:のiOS:ダッシュ

2017-06-14T15:38.000Z 

は、私は次の実装を試してみました:

-(void)currentDate 
{ 
    NSDate *date = [NSDate date]; 
    NSDateFormatter *formatter = [NSDateFormatter new]; 
    NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone]; 
    formatter.timeZone = destinationTimeZone; 
    [formatter setDateStyle:NSDateFormatterLongStyle]; 
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.ZZZ"]; 
    NSString *dateString = [formatter stringFromDate:date]; 
    NSLog(@"%@",dateString); 
} 

しかし、これは私が得るフォーマットの文字列です:

2017-06-14T14:08:56.-0700 

どのようにm y NSDateFormatterを使用して、次の日付形式を取得します。

2017-06-14T15:38.000Z 

本当にありがとうございます。

+0

.000Zはミリ秒ではありませんが、UTCの時刻にdiference尊重 –

+0

@ReinierMelianである、あなたは説明できますか? – user2924482

+0

この形式はyyyy-MM-dd'T'HH:mm:ss.ZZZは年、月、日、時、時、分、秒であり、ZZZはUTC時間との時差です。 -7hrsの違いがあります –

答えて

1

NSDateFormatterのタイムゾーンをsystemTimeZoneと設定しています。したがって、出力は常にあなたのデバイスのタイムゾーンになります(-7h w.r.t UTC時間)。

は、ここで修正します:

-(void)currentDate 
{ 
    NSDate *date = [NSDate date]; 
    NSDateFormatter *formatter = [NSDateFormatter new]; 
    NSTimeZone *destinationTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; 
    formatter.timeZone = destinationTimeZone; 
    [formatter setDateStyle:NSDateFormatterLongStyle]; 
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.ZZZ"]; 
    NSString *dateString = [formatter stringFromDate:date]; 
    NSLog(@"%@",dateString); 
} 

OUTPUT:

2017-06-15T05:11:35.+0000 
関連する問題