According to Zaph's comment in another answer: the maximum number S is 3. Any others just produce zeros.
eg.
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSSSSS"; // Last 3 'S' ignored. Then @"2011-04-21 03:31:37.311396" will produce 2011-04-21 03:31:37.311000
To maintain full microsecond accuracy, try this magic:
-(NSDate *)_dateFromUtcString:(NSString *)utcString{ if(!utcString){ return nil; } static NSDateFormatter *df = nil; if (df == nil) { df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; } NSArray* parts = [utcString componentsSeparatedByString:@"."]; NSDate *utcDate = [df dateFromString:parts[0]]; if(parts.count > 1){ double microseconds = [parts[1] doubleValue]; utcDate = [utcDate dateByAddingTimeInterval:microseconds / 1000000]; } return utcDate; }
Now NSString "2011-04-21 03: 31: 37.310396" will fully analyze NSDate 2011-04-21 03: 31: 37.310396
malhal
source share