Re: NSCalendar and the number of days in a week in a month?
Re: NSCalendar and the number of days in a week in a month?
- Subject: Re: NSCalendar and the number of days in a week in a month?
- From: Chris Kane <email@hidden>
- Date: Mon, 8 May 2006 11:09:52 -0700
Once you have your firstSaturday components, if you print out the
actual date, you'll probably find it's not the date you're looking for:
#define ALL (NSEraCalendarUnit|NSYearCalendarUnit |
NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|
NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|
NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)
NSDateComponents *fullComponents = [calendar components:ALL fromDate:
[calendar dateFromComponents: firstSaturdayOfCurrentMonth]];
NSLog(@"%@", [calendar dateFromComponents: fullComponents]);
"Week" in NSCalendar refers to "week of year", rather than "week of
month", in more the ISO or European business style.
If you want the first Saturday, the weekday ordinal is what you want.
NSDateComponents *firstSat = [[NSDateComponents alloc] init];
[firstSat setYear:2006];
[firstSat setMonth:11];
[firstSat setWeekdayOrdinal:1]; // first
[firstSat setWeekday:7]; // Saturday
NSDateComponents *fullComponents = [cal components:ALL fromDate:[cal
dateFromComponents: firstSat]];
NSLog(@"%@", [cal dateFromComponents: fullComponents]);
2006-05-08 10:52:19.276 a.out[28606] 2006-11-04 00:00:00 -0800
At this point, [fullComponents day] gives you the number of days in
the first week, according to the definition you seem to be aiming for.
Now, Saturday is not necessarily the last day of the week for the
user. Also, a week is not necessarily 7 days in every calendar, and
so on for other assumptions. The safest approach is probably to
compute the first day of the month:
NSDateComponents *firstDay = [[NSDateComponents alloc] init];
[firstDay setYear: targetYear];
[firstDay setMonth: targetMonth];
[firstDay setDay:1];
NSDate *firstDate = [cal dateFromComponents: firstDay];
NSLog(@"%@", firstDate);
then creep forward by days until the week number changes (note: not
"increases", but "changes"):
unsigned int weekNum = [[cal components: NSWeekCalendarUnit
fromDate: firstDate] week];
NSDate * nextDate = firstDate, *curDate;
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
unsigned int newWeekNum;
do {
curDate = nextDate;
nextDate = [cal dateByAddingComponents:oneDay toDate: curDate
options:0];
newWeekNum = [[cal components: NSWeekCalendarUnit fromDate:
nextDate] week];
} while (newWeekNum == weekNum);
// Now, nextDate is the first day of the next week, curDate is
the last day of the previous week
NSLog(@"%@", nextDate);
NSLog(@"%@", curDate);
However, that's a bit over-thorough, and I think it should be
sufficient to add a week to the first day and regress to the day
before the start of the next week (so, instead of the block of code
just above):
NSDateComponents *oneWeek = [[NSDateComponents alloc] init];
[oneWeek setWeek:1];
NSDate *nextDate = [cal dateByAddingComponents:oneWeek toDate:
firstDate options:0];
NSRange r = [cal rangeOfUnit: NSDayCalendarUnit inUnit:
NSWeekCalendarUnit forDate: nextDate];
// (r.location - 1) is now also your "number of days in first week"
NSDateComponents *targetDay = [firstDay copy];
[targetDay setDay: r.location - 1];
NSDate *targetDate = [cal dateFromComponents: targetDay];
NSLog(@"%@", targetDate);
and then targetDate should be the first moment of the last day of the
first week of the target month, where the "first week" is the one
that contains day 1.
Chris Kane
Cocoa Frameworks, Apple
On May 8, 2006, at 6:59 AM, Nick Zitzmann wrote:
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
_______________________________________________
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