Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

objective c - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...