I would like to get a NSString from a date, with a date format like this:
MMEEEY ww
The problem is for the 'ww', the week of year. I have to compare this format with another, taken from a backoffice. But the first weekday of the backoffice is Monday, whereas the iOS one is Sunday. So, when it's Sunday, the string is not the same because the week of the year is not the same.
The question is: How to force NSDateFormatter to use Monday as first weekday?
I tried to set the calendar property of NSDateFormatter with a calendar with the firstWeekday property set to 2, but it doesn't work.
The gist of this:
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.firstWeekday = 2;
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.calendar = calendar;
Edit: the test after Noa's answer
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
dateFormatter.locale = locale;
dateFormatter.dateFormat = @"MMEEEY ww";
NSDate * date = [NSDate dateWithTimeIntervalSince1970:1330251642]; // next sunday
NSLog(@"%@ -> %@", date, [dateFormatter stringFromDate:date]);
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.firstWeekday = 2;
dateFormatter.calendar = calendar;
NSLog(@"%@ -> %@", date, [dateFormatter stringFromDate:date]);
Result:
2012-02-24 11:31:27.878 Test[2546:15203] 2012-02-26 10:20:42 +0000 -> 02Sun2012 09
2012-02-24 11:31:27.880 Test[2546:15203] 2012-02-26 10:20:42 +0000 -> 02Sun2012 09