Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 531
ageektrapped Avatar asked Jun 23 '11 15:06

ageektrapped


People also ask

How do you format milliseconds?

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.

What format is this date string?

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.

What is string date/time format?

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.


2 Answers

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

like image 141
Simon Whitaker Avatar answered Sep 28 '22 18:09

Simon Whitaker


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

like image 38
Caleb Avatar answered Sep 28 '22 16:09

Caleb