Dilshan
Dilshan

Reputation: 3221

NSDateFormatter dateFromString give wrong result

I have the following code.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/d/YYYY"];
NSDate *addedDate = [dateFormat dateFromString:weightDTO.dateAdded];
[dateFormat release];
NSLog(@"dateAdded %@", weightDTO.dateAdded);
NSLog(@"addedDate %@", addedDate);

This print the following in a loop with 2 entries.

dateAdded 06/2/2012
addedDate 2011-12-18 18:30:00 +0000

dateAdded 06/8/2012
addedDate 2011-12-18 18:30:00 +0000

So it is supposed to get a 2012 date for addedDate object. However the result is different. What am I doing wrong here?

Thank you

Upvotes: 1

Views: 215

Answers (1)

saadnib
saadnib

Reputation: 11145

change your dateFormat to this -

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

Upvotes: 3

Related Questions