Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter dateFromString will not parse a particular date string

Good day,

i have an NSDateFormatter that doesn't seem to like a particular string that i am trying to convert. this particular instance is

2009-10-16T09:42:24.999605

All of my other dates are formatted the same, and can be parsed.

Here is the code that converts the string in the 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 is nil after the dateFromString call.

Has anyone else come across cases where their dates could not converted into NSDates?

Thanks,

like image 676
cnickelwebcom Avatar asked Oct 16 '09 14:10

cnickelwebcom


2 Answers

I was able to get the date to parse by removing the subsecond field from the formatter:

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 you can find out in greater detail why it failed with the subsecond field using this function as it looks like it does the same thing, but 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:

At any rate, hopefully this gets you going again.

like image 200
Epsilon Prime Avatar answered Nov 09 '22 23:11

Epsilon Prime


You should try lowercase "s" in decimals place, like this:

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

It worked for me.

like image 45
Mmosa Avatar answered Nov 10 '22 01:11

Mmosa