我在 iPhone 上的日期字符串中使用的毫秒格式字符串是什么?

我被要求以包含毫秒的格式解析字符串。我用什么格式的字符串来获得正确的日期值?

例如,假设我有一个值如下的字符串: “2011-06-23T13:13:00.000”

在下面的代码中传递什么格式字符串给我的 NSDateFormatter?

NSString *dateValue = @"2011-06-23T13:13:00.000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *formatString = @"yyyy-MM-dd'T'HH:mm:ss.???";
[formatter setDateFormat:formatString];
NSDate *date = [formatter dateFromString:dateValue];

在上面的代码中,我使用什么来代替?

44132 次浏览

use SSS for three decimal places of a second

"yyyy-MM-dd'T'HH:mm:ss.SSS"

It's SSS, per the Unicode Locale Data Markup Language spec.

"yyyy-MM-dd'T'HH:mm:ss.SSS"

More generally, use any number of upper-case S characters to get that many decimal places in the fractions-of-a-second component. (So ss.S will show the time to the nearest tenth of a second, for example.)

The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.

You need to pass same number of 'S' at the end for example My Date was '2015-11-17T03:36:45.041503Z' the i used 'yyyy-MM-dd'T'HH:mm:ss.SSSSSSz' formatter, I mean see the number of 'S' is 6 as 6 digit coming in date.