iosnsdateformatter

NSDateFormatter milliseconds bug


I want to create a NSDateFormatter that should parse dates like this "2014-05-13 23:31:41.374577". So:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSSSSS";
NSString *dateString = @"2014-05-13 23:31:41.374577";
NSDate *date = [formatter dateFromString:dateString];
NSString *anotherDateString = [formatter stringFromDate:date];

However, the anotherDateString is 2014-05-13 23:31:41.374000. Why does it truncate the milliseconds?


Solution

  • It seems that NSDateFormatter works only with millisecond resolution, for the following reasons: