Reputation: 339
-(NSInteger)getTheUltimateWeekOfAYear:(NSInteger)year
{
NSDateComponents *comps = [NSDateComponents alloc];
[comps setDay:31];
[comps setMonth:12];
[comps setYear:year];
NSCalendar *usersCalendar =[[NSLocale currentLocale] objectForKey:NSLocaleCalendar];
NSDate *date = [usersCalendar dateFromComponents:comps];
NSDateComponents *weekComps =
[usersCalendar components:NSWeekCalendarUnit fromDate:date];
return [weekComps week];
}
`
Hi, This snippet wrongly return 1 when run on the iOS 5.1 Simulator. I believe it should return 53. What am I doing wrong? BTW my Simulator is preset to gregorian calendar. /John
Upvotes: 1
Views: 254
Reputation: 42414
It depends on your context. From the wikipedia:
ISO 8601 includes the ISO week date system, a numbering system for weeks – each week begins on a Monday and is associated with the year that contains that week's Thursday (so that if a year starts in a long weekend Friday–Sunday, week number one of the year will start after that). For example, week 1 of 2004 (2004W01) ran from Monday 29 December 2003 to Sunday, 4 January 2004, because its Thursday was 1 January 2004, whereas week 1 of 2005 (2005W01) ran from Monday 3 January 2005 to Sunday 9 January 2005, because its Thursday was 6 January 2005 and so the first Thursday of 2005. The highest week number in a year is either 52 or 53 (it was 53 in the year 2004).
In some countries, though, the numbering system is different from the ISO standard.
Upvotes: 3