Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
- Subject: Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
- From: mmalc Crawford <email@hidden>
- Date: Mon, 22 Dec 2008 08:47:36 -0800
On Dec 22, 2008, at 8:34 AM, Keith Blount wrote:
Hi,
Many thanks to both of you for your very helpful replies - much
appreciated! I've gone with Ken's solution, which works perfectly
for what I need. For the sake of the archives, I've attached the
method I created based on Ken's code, which just creates a
hierarchical dictionary of objects with the hierarchy Year->Month-
>Day, with a "Title" key for each object, so that it can be used
easily enough in the data source an NSOutlineView (which is how I'll
be using it).
Thanks again - and happy holidays.
All the best,
Keith
-- Ken's code modified for use with an NSOutlineView data source or
suchlike --
- (NSMutableDictionary *)diarySpacesForYear:(int)year
{
NSString *yearStr = [NSString stringWithFormat:@"%i", year];
NSDate *startDate = [NSDate dateWithNaturalLanguageString:[NSString
stringWithFormat:@"January 1 %i", year]];
NSDate *endDate = [NSDate dateWithNaturalLanguageString:[NSString
stringWithFormat:@"January 1 %i", year+1]];
Per the documentation, use of dateWithNaturalLanguageString is
strongly discouraged.
Don't derive dates like this in code, use NSDateComponents...
while ([startDate timeIntervalSinceDate:endDate] < 0)
This is simply horrible.
If you want to perform calendrical calculations, again use
NSDateComponents:
<http://developer.apple.com/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html
>
More examples are given in this version:
<http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html
>
If you just want an array of month names, use monthSymbols (or
standaloneMonthSymbols, or any of the related methods):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// set locale and/or calendar as appropriate
NSArray *monthSymbols = [dateFormatter monthSymbols];
Your original suggestion:
1) Start with a January NSDate in the specified year.
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).
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.
is basically the correct way to approach this.
There shouldn't be any need, though, to "add a month" for each
iteration, just start a new month with a date components object with a
new month number.
mmalc
_______________________________________________
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