Cyupa
Cyupa

Reputation: 1135

NSDateFormetter losing an year

I was using NSDateFormatter to convert a date NSString instance to a NSDate object. At a point, I realized that something isn't right, when I did a check, it seemed like NSDateFormatter was loosing a whole year when converting from a string to a NSDate.

The code looks like this:

NSString *dateString = [dictionary objectForKey:@"match_date_time"];
if (dateString) {
     // dateString is "2012-10-30 04:30:00"

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
     [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

     self.date = [formatter dateFromString:dateString];
     // self.date ends up 2011-10-30 04:30:00 +0000, yes 2011 not 2012.
}

Does anyone know why does NSDateFormatter fails so miserably?

Thanks!

Upvotes: 0

Views: 78

Answers (1)

freelancer
freelancer

Reputation: 1658

just write below code

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Upvotes: 1

Related Questions