• 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
NSCalendar and the number of days in a week in a month?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSCalendar and the number of days in a week in a month?


  • Subject: NSCalendar and the number of days in a week in a month?
  • From: Nick Zitzmann <email@hidden>
  • Date: Mon, 8 May 2006 07:59:47 -0600

How do I, using NSCalendar, obtain the number of days in a given week in a given month? I'm trying to figure out how to count the number of days in the first week of a given month, and I can't seem to figure it out. Here's what I'm trying:

NSDateComponents *firstSaturdayOfCurrentMonth = [[NSDateComponents alloc] init];
int numberOfDaysInFirstWeekOfMonth;


[firstSaturdayOfCurrentMonth setEra:[currentDateComponents era]];
[firstSaturdayOfCurrentMonth setYear:[currentDateComponents year]];
[firstSaturdayOfCurrentMonth setMonth:[currentDateComponents month]];
[firstSaturdayOfCurrentMonth setWeek:1];
[firstSaturdayOfCurrentMonth setWeekday:7];
numberOfDaysInFirstWeekOfMonth = [[calendar components:NSDayCalendarUnit fromDate:[calendar dateFromComponents:firstSaturdayOfCurrentMonth]] day];
[firstSaturdayOfCurrentMonth release];


...where currentDateComponents is an NSDateComponents object that has already been initialized and set to the components of a given date using NSCalendar.

My method returns the correct amount of days in the first week of May 2006 (6), but returns the incorrect number for November 2006 (it returns 6 when there's really 4). I also tried using - rangeOfUnit:inUnit:forDate: and it always returned 7 as a length.

Nick Zitzmann
<http://www.chronosnet.com/>


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: NSCalendar and the number of days in a week in a month?
      • From: Chris Kane <email@hidden>
    • Re: NSCalendar and the number of days in a week in a month?
      • From: Greg Herlihy <email@hidden>
  • Prev by Date: Re: Menu Bar Application
  • Next by Date: Re: filling NSImage with binary data from NSData
  • Previous by thread: Re: Menu Bar Application
  • Next by thread: Re: NSCalendar and the number of days in a week in a month?
  • Index(es):
    • Date
    • Thread