views:

59

answers:

2

Hi,

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

A: 

You should user NSDateFormatter like so:

NSDateFormatter *fm = [[NSDateFormatter alloc] initWithDateFormat:@"ww" allowNaturalLanguage:NO];
NSString *week = [fm stringFromDate: date];
Max Seelemann
Thanks for your reply. Your code is intended for Mac OS X. Now I tried to do it (see edited question), but I'm getting 35. Outlook states 36. The first Jannuary is also in the first week in Outlook. From where comes the difference?
testing
May depend on the beginning of the week. In the US it's common to start with sunday as the first day of the week, in europe it's monday. Maybe outlook does only either style?
Max Seelemann
Thanks for the link to date formats. After what I read the first US calendar week is the week which contains the 1st Jannuary. And this year Friday was the 1st Jannuary. So it doesn't matter if Sunday or Monday is the first day of the week in this case. I don't know what Outlook is using (it's not the ISO because the first week should contain the 4th Jannuary). Also I don't know which these classes from Apple are doing. Suggestions? I've now tried [cal setFirstWeekday:2]; and I get as expected 35. Your posted link conforms after ISO 8601 (so four days of the new year should be the 1st week)
testing
A: 

Use an NSCalendar and NSDateComponents.

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
NSInteger week = [components week];
Elfred
I had to declare NSDate *date = [NSDate date]; Still one problem remains. How can I influence the result if it is 35 or 36. DIN/ISO states the first week includes the 4th Jannuary. Outlook for example states that 1st, 2nd, 3rd Jannuary 2010 (Friday to Sunday) is in the first week (US calendar week?). So there are differences everywhere and I want as a result 36 according to Outlook.
testing
Calendar is actually based on Locale. In the US it defaults to the Gregorian calendar. It seems like apple is planing on implementing the ISO8601 calendar, but it isn't done yet. They have a NSISO8601Calendar key to initialize NSCalendar with, but the documentation says it isn't implemented.
Elfred
One thing you can do is check what's the week of the 4th of january and alter the week above based on that.
Elfred
I see I have to do it myself. Thanks for your hints. Still one thing I don't understand: How Apple calculates the 35? I can't see any rules for that ...
testing
When you say Outlook, are you referring to Microsoft Outlook? Also, do you know which system it uses to count. If you think about it, it seems like Apple is using the ISO8601 way of counting dates. This is the 35th week of the year if you start counting from the week that includes January 4th. Otherwise, it would be the 36th week.
Elfred
Yes, Microsoft Outlook. I don't know how the week at Outlook is counted. I had also a look in my desk calender. It also stated 35. So I think Outlook is doing something not standardized here.
testing