• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Week of month confusion?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Week of month confusion?


  • Subject: Week of month confusion?
  • From: Izak van Langevelde <email@hidden>
  • Date: Tue, 19 Mar 2013 14:21:30 +0100

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Week of month confusion?
      • From: Kyle Sluder <email@hidden>
    • Re: Week of month confusion?
      • From: Fulbert Boussaton <email@hidden>
  • Prev by Date: Re: Understanding user defaults
  • Next by Date: Re: Week of month confusion?
  • Previous by thread: More prefs/iCloud KVS questions
  • Next by thread: Re: Week of month confusion?
  • Index(es):
    • Date
    • Thread