Getting the last date of the month?
Getting the last date of the month?
- Subject: Getting the last date of the month?
- From: Nick Zitzmann <email@hidden>
- Date: Mon, 19 Mar 2007 21:33:28 -0600
I'm currently using the following strategy to get an NSDate pointing
to the last day in the month: (firstDayOfMonth is an NSDate that is
pointing to the first day in the month, calendar is an NSCalendar)
dateComponents = [calendar components:(NSEraCalendarUnit |
NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:firstDayOfMonth];
[dateComponents setDay:[calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit forDate:firstDayOfMonth].length];
lastDayOfMonth = [calendar dateFromComponents:dateComponents];
This code makes the assumption that all days in the month are
contiguous. 99.9999999...% of the time that is true, but in the month
of October 1582 that is not true, for reasons we recently discussed
on the list (it's the month of the Julian->Gregorian calendar switch-
over).
Does anyone have a better method of figuring out how many days are in
a month that does not involve creating an expensive loop, and makes
no assumptions like the above? If so, then what did you do?
(IMPORTANT: It has to work with calendars other than the Gregorian
calendar, so always using 31 for October is not an acceptable solution.)
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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