1
我想一個字符串,它看起來像這樣(如果是完全正確的RFC3339格式不知道)轉換:轉換RFC3339?字符串的NSDate
2014-07-07T16:11:02.085Z
要一個NSDate。我環顧四周,嘗試了多種格式,並使用蘋果的代碼進行轉換。下面是格式我一直在努力:
@"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);
問題是什麼? –