I'm working on a small week-based calendar, but for some reason, the week 
number within the month of the first day of the month is either 0 or 1, while I 
would expect it to be 1.

        NSDateComponents *components = [[NSDateComponents alloc] init];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        [formatter setDateStyle:NSDateFormatterLongStyle];
        
        [components setDay:1];
        [components setYear:2013];

        for(int i=1;i<=12;i++){
            [components setMonth:i];
            NSDate *date=[[NSCalendar currentCalendar] 
dateFromComponents:components];
            NSLog(@"Date %@ falls in week %ld of the month", 
                  [formatter stringFromDate:date] , 
                  [[[NSCalendar currentCalendar] 
components:NSWeekOfMonthCalendarUnit fromDate: date] weekOfMonth]); 
        }

Output:

2013-03-19 13:32:32.777 Admin[3543:707] Date 1 January, 2013 falls in week 1 of 
the month
2013-03-19 13:32:32.778 Admin[3543:707] Date 1 February, 2013 falls in week 0 
of the month
2013-03-19 13:32:32.779 Admin[3543:707] Date 1 March, 2013 falls in week 0 of 
the month
2013-03-19 13:32:32.780 Admin[3543:707] Date 1 April, 2013 falls in week 1 of 
the month
2013-03-19 13:32:32.781 Admin[3543:707] Date 1 May, 2013 falls in week 1 of the 
month
2013-03-19 13:32:32.781 Admin[3543:707] Date 1 June, 2013 falls in week 0 of 
the month
2013-03-19 13:32:32.782 Admin[3543:707] Date 1 July, 2013 falls in week 1 of 
the month
2013-03-19 13:32:32.783 Admin[3543:707] Date 1 August, 2013 falls in week 0 of 
the month
2013-03-19 13:32:32.783 Admin[3543:707] Date 1 September, 2013 falls in week 1 
of the month
2013-03-19 13:32:32.784 Admin[3543:707] Date 1 October, 2013 falls in week 1 of 
the month
2013-03-19 13:32:32.785 Admin[3543:707] Date 1 November, 2013 falls in week 0 
of the month
2013-03-19 13:32:32.786 Admin[3543:707] Date 1 December, 2013 falls in week 1 
of the month

Am I overlooking some subtlety in the definition of weekOfMonth?

Thanks,
Izak
---
Grinnikend door het leven...

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to