NSCalendar and the number of days in a week in a month?
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