NSDateFormatter dateFromString will not parse a specific date string

Good day,

I have an NSDateFormatter which does not seem to look like the specific string I'm trying to convert. this particular instance

 2009-10-16T09:42:24.999605 

All my other dates are formatted the same and can be analyzed.

Here is the code that converts string to NSDate .

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSS"]; [order setModifiedAtDate:[dateFormatter dateFromString:[orderDataDictionary valueForKey:@"ModifiedAt"]]]; 

order.ModifiedAtDate nil after calling dateFromString .

Does anyone else encounter cases where their dates cannot be converted to NSDates ?

Thanks,

+6
iphone nsdate nsdateformatter
source share
2 answers

I was able to get the date for parsing by removing the sub-section field from the formatting:

 NSString *test = @"2009-10-16T09:42:24.999605"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; NSDate *testOut = [dateFormatter dateFromString:test]; 

I suspect that you can learn in more detail why this was not possible using the subgrid field using this function, since it looks like it is doing the same thing, but it returns error messages:

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/getObjectValue:forString:range:error :

Anyway, hopefully this will make you go again.

+7
source share

You should try the lowercase "s" in decimal value, for example:

 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.ssssss"] 

It worked for me.

+1
source share

All Articles