Re: NSCalendar/NSDate - generating all months/days in a year
Re: NSCalendar/NSDate - generating all months/days in a year
- Subject: Re: NSCalendar/NSDate - generating all months/days in a year
- From: Nathan Vander Wilt <email@hidden>
- Date: Mon, 22 Dec 2008 11:22:07 -0600
On Dec 22, 2008, at 4:53 AM, Keith Blount wrote:
Hi,
Apologies in advance for what I think must be a basic question. It's
something I've never had cause to do before, assumed must be fairly
straightforward, and then seemed a lot more complicated than it
should be which leads me to think that I am using the wrong search
terms...
All I want to do is this: I would like to generate the names of all
the months for a specified year. Then for each month in that year, I
would like to generate the names of every day. For instance:
2009
- January
-- Thursday, 1st January 2009
-- Friday, 2nd January 2009
... and so on
- February
-- ... etc.
I seem to be looking in the wrong areas in the frameworks, though. I
thought NSDate and NSCalendar would be the place to look, but both
seem overly complicated for this purpose. As far as I can see, in
order to use NSCalendar to do this, I would have to do the following:
I would not agree with the other two posters on this, though their
solutions should work in practice for your situation if you don't need
anything general purpose. Otherwise, NSCalendar is your friend; if you
find yourself typing something like January = 31 or 6 = "Friday" or 60
* 60 * 24 in your code you're likely duplicating its functionality
without getting any of its benefits (language issues, and political
issues like daylight savings, leap years, leap seconds, etc.) The way
people label time is a complicated, changeable thing, best to let the
operating system handle it for you.
1) Start with a January NSDate in the specified year.
[the following code is all typed up in Mail, so may not be quite
working]
Sounds good, and if you know the year the easiest way would be:
NSDateComponents* components = [[NSDateComponents alloc] init];
[components setMonth:1];
[components setYear:specifiedYear];
[calendar dateFromComponents:components];
Otherwise, if you have a date within the year:
NSDate* startOfYear = nil;
[calendar rangeOfUnit:NSYearCalendarUnit startDate:&startOfYear
interval:NULL forDate:dateWithinYear];
2) Get number of months for that year using [calendar
rangeOfUnit:NSMonthCalendarUnit inUnit:NSYearCalendarUnit
forDate:january] (given that the app is only for personal purposes
for now, I could just assume 12 for this part).
You could do this, or just iterate as described below until the year
changes.
3) Get number of days in each month by using [calendar
rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit
forDate:january] and then adding on a month (though how to do that?
- NSTimeInterval is in seconds and there are different numbers of
days in each month...) to get the number of days for each month.
You can easily add on a month or day at a time using NSDateComponents
and calendar. You can either use NSCalendar's -
dateByAddingComponents:toDate:options:, or what I might do is take the
component you might have made (or can get) from the original year
+January and add days directly to that. NSCalendar can handle
overflow, so you can have an NSDateComponent that says something like
Year:2008 Month:1 Days:364 and you will get the right date from
components. (Which you can then convert back to normalized components
if you want to, say, check if the month has changed since the last day.)
4) Use NSDateFormatter to format the date for display (I'll have to
do that part no matter what method I use).
Yes, among other benefits, this saves you from having to localize all
the month names and such yourself.
Steps 1-3 seem needlessly complicated and error-prone, though, which
leads me to suspect I am taking completely the wrong approach and
missing the blindingly obvious. Is there a better way of doing this?
I think your steps were on the right track, I've just tried to point
out some other helpful methods. NSCalendar is not always exactly
intuitive or succinct to use, but it does a LOT for you and even if
this project is simple, getting familiar with it now will be a win if
you ever do something needing more accuracy in the future.
hope this helps,
–natevw_______________________________________________
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