Reputation: 123
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
Upvotes: 5
Views: 2546
Reputation: 17208
Based on some testing, the date formatter does seem to honor the calendar setting. Maybe you could give examples of what dates you're trying to format, the results you want, and the results you're getting.
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMEEEY ww"];
NSDate *date;
date = [NSDate dateWithString:@"2012-01-01 12:00:00 +0000"];
NSLog(@"%@ -> %@", date, [df stringFromDate:date]);
date = [NSDate dateWithString:@"2012-01-02 12:00:00 +0000"];
NSLog(@"%@ -> %@", date, [df stringFromDate:date]);
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.firstWeekday = 2;
df.calendar = calendar;
date = [NSDate dateWithString:@"2012-01-01 12:00:00 +0000"];
NSLog(@"%@ -> %@", date, [df stringFromDate:date]);
date = [NSDate dateWithString:@"2012-01-02 12:00:00 +0000"];
NSLog(@"%@ -> %@", date, [df stringFromDate:date]);
Results:
2012-02-22 12:42:17.020 test[68385:207] 2012-01-01 12:00:00 +0000 -> 01Sun2012 01
2012-02-22 12:42:17.021 test[68385:207] 2012-01-02 12:00:00 +0000 -> 01Mon2012 01
2012-02-22 12:42:17.022 test[68385:207] 2012-01-01 12:00:00 +0000 -> 01Sun2011 52
2012-02-22 12:42:17.022 test[68385:207] 2012-01-02 12:00:00 +0000 -> 01Mon2012 01
I'm not sure whether you want 1/1/12 part of 2011's week 52, but again, that's why I'm asking what results you want.
Upvotes: 1