Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dateformatter change the year of my date

I try to display a date with a dateformatter but this one change the year of the date.

This is the method :

setLabelWithDate:(NSdate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:gmt];    
    [dateFormatter setDateFormat:@"dd MMMM YYYY"];

    NSLog(@"Date : %@",date);
    NSLog(@"stringFromDate %@",[dateFormatter stringFromDate:date]);

    self.selectedDateLabel.text = [dateFormatter stringFromDate:date];
    [dateFormatter release];
}

And this is what is displayed in the console :

2012-05-04 13:08:50.442 MyAppli[3339:fb03] Date : 2012-12-30 00:00:00 +0000
2012-05-04 13:08:50.443 MyAppli[3339:fb03] stringFromDate 30 December 2013

Here I m. The dateFormatter add myDate one year and I really don't know why. This case happens only for the 2 last dates of the year (30 and 31 december).

Thanks for your help.

Alexandre

like image 529
Alexandre Avatar asked May 04 '12 11:05

Alexandre


1 Answers

Use

[dateFormatter setDateFormat:@"dd MMMM yyyy"]; // lowercase y

and you probably should check this and this.

like image 159
Jonas Schnelli Avatar answered Sep 29 '22 09:09

Jonas Schnelli