testing testing - 3 months ago 21
Objective-C Question

calculate calendar week

how can I calculate the calendar week? A year has 52/53 weeks and there are two rules:

-USA

-DIN 1355 / ISO 8601

I'd like to work with DIN 1355 / ISO 8601. How can I manage that?

Edit:

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"ww"];
NSString *weeknumber = [dateFormat stringFromDate: today];
NSLog(@"week: %@", weeknumber);


Taken from http://iphonedevelopertips.com/cocoa/date-formatter-examples.html

Where do I find the allowed date formats? I can't find it in the documentation ...

Thanks

Answer

Use an NSCalendar and NSDateComponents.

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
NSInteger week = [components week];