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?
In the Format Cells window, go to the Number tab, select Custom from the Category list, and enter h:mm:ss. 000 in the Type text box. As a result, all of the time values are displayed with milliseconds as decimals.
The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.
A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With