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?
It seems that
NSDateFormatter
works only with millisecond resolution, for the following reasons:By setting a breakpoint in
CFDateFormatterCreateDateFromString
, one can see that this function is called fromdateFromString:
:CFDateFormatterCreateDateFromString()
is from CFDateFormatter.c which is open source. One can see that all calendrical calculations are made using the ICU Calendar Classes.It is stated in calendar.h that
Calendar
usesUDate
which has a millisecond resolution: