NSDateFormatter and setDateFormat: and SSS
Using NSDateFormatter, here is my 开发者_运维技巧input :
00:20:11,026 (NSString)
And here is the output i want :
"00:20:11,026"; (NSDate)
but what I obtained is :
"1970-01-01 00:20:11 +0100" (NSDate)
// SSS is missing and there is too much information...
Here is what i coded :
...
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm:ss,SSS"];
[dateFormatter dateFromString:@"00:20:11,026"];
...
Well, first of all, your NSDateFormatter is just fine.
Judging by the output, it looks like you're just NSLogging an actual NSDate object, and expecting the format to be representative of the NSDateFormatter that was used to create it. All NSDate objects will be formatted the same if you NSLog them, regardless of whether you got them from an NSDateFormatter or not. The "1970-01-01 00:20:11 +0100" string that you are worried about is actually supplied by NSDate's implementation of the -description
method.
So my guess is that, while the fractional seconds (SSS) are missing from your log statement, they actually are present and accounted for in the NSDate object. Furthermore, the "too much information" is also just an artifact of the default description provided by NSDate. I'm fairly certain your actual NSDate contains all of the information you want it to.
精彩评论