1

次のような文字列を変換しようとしています (RFC3339 形式が完全に正しいかどうかはわかりません)。

2014-07-07T16:11:02.085Z

NSDate へ。私は周りを見回して複数のフォーマットを試し、Apple のコードを使用して変換しています。これが私が試してきたフォーマットです:

@"yyyy'-'MM'-'dd'T'HH':'mm':'sss'Z'" //apples
@"yyyy-MM-dd'T'HH:mm:ss.'999Z'"
@"yyyy-MM-dd'T'HH:mm:ss.sss'Z'"
@"yyyy-MM-dd'T'HH:mm:ss"

私はそれについてどのように行っていますか:

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:@"2014-07-07T16:11:02.085Z"];

NSLog(@"date: = %@", date);

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);

    [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}

NSLog(@"userVisibleDateTimeString = '%@'", userVisibleDateTimeString);
4

1 に答える 1