Ethan Allen
Ethan Allen

Reputation: 14835

Why can I not get the Year from a NSString using NSDateFormatter dateFromString?

Here is my code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];

if ([dateFormatter dateFromString:@"11/09/2010"])
{
   //PASS
}
else
{
   //FAIL
}

Why is it going into the FAIL section every time?

Upvotes: 0

Views: 74

Answers (1)

danielbeard
danielbeard

Reputation: 9149

Try setting the date format like this:

[dateFormatter setDateFormat:@"dd/MM/yyyy"];

Or the other way around, depending on which field is days and which is months.

Upvotes: 1

Related Questions