testing testing - 1 year ago 111
Objective-C Question

calculate calendar week

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


-DIN 1355 / ISO 8601

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


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 ...


Answer Source

Use an NSCalendar and NSDateComponents.

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
NSInteger week = [components week];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download