What format string do I use for milliseconds in date strings on iPhone?

IphoneObjective C

Iphone Problem Overview


I'm required to parse strings in a format that includes milliseconds. What format string do I use to get the right date value?

For example, suppose I have a string with the following value: "2011-06-23T13:13:00.000"

What format string do I pass to my NSDateFormatter in the following code?

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];

What do I use in place of ??? in the code above?

Iphone Solutions


Solution 1 - Iphone

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.)

Solution 2 - Iphone

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

Solution 3 - Iphone

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.

Solution 4 - Iphone

use SSS for three decimal places of a second

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionageektrappedView Question on Stackoverflow
Solution 1 - IphoneSimon WhitakerView Answer on Stackoverflow
Solution 2 - IphoneCalebView Answer on Stackoverflow
Solution 3 - IphonePrakash RajView Answer on Stackoverflow
Solution 4 - IphoneharryoversView Answer on Stackoverflow