• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSCalendar/NSDate - generating all months/days in a year
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCalendar/NSDate - generating all months/days in a year


  • Subject: Re: NSCalendar/NSDate - generating all months/days in a year
  • From: Ken Tozier <email@hidden>
  • Date: Mon, 22 Dec 2008 07:20:11 -0500

Fun problem

Basically it's safe to assume 24 hours in a day for date calculation (check details at Wikipedia) so you just have to get January 1 of the target year, January 1 of the subsequent year and increment until the time interval between them equals zero. This makes no assumptions about how many days there are in a year so should work fine for leap years as well.

NSMutableArray	*result			= [NSMutableArray array];

NSDate *startDate = [NSDate dateWithNaturalLanguageString: @"January 1 2009"],
*endDate = [NSDate dateWithNaturalLanguageString: @"January 1 2010"];

NSString *currentMonth = @"January",
*testMonth;

int secondsPerDay = 60 * 60 * 24;


// init list
[result addObject: currentMonth];
[result addObject: startDate];

// loop until interval >= 0
while ([startDate timeIntervalSinceDate: endDate] < 0)
{
// get the next day
startDate = [startDate addTimeInterval: secondsPerDay];

// get month from date
testMonth = [startDate descriptionWithCalendarFormat: @"%B"
timeZone: [NSTimeZone localTimeZone]
locale: [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];

// if month is different add it to the list
if (![testMonth isEqualToString: currentMonth])
{
[currentMonth release];
currentMonth = [testMonth retain];
[result addObject: currentMonth];
}

// add date to list
[result addObject: startDate];
}


NSLog(@"result: %@", result);

You could easily group days in each month by tweaking it slightly


On Dec 22, 2008, at 5: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:

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.
4) Use NSDateFormatter to format the date for display (I'll have to do that part no matter what method I use).


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?

Many thanks in advance for any help or pointers.
All the best,
Keith



_______________________________________________

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

_______________________________________________

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


References: 
 >NSCalendar/NSDate - generating all months/days in a year (From: Keith Blount <email@hidden>)

  • Prev by Date: Re: Saving the project
  • Next by Date: Re: Reliable way to capitalize camel-case strings?
  • Previous by thread: NSCalendar/NSDate - generating all months/days in a year
  • Next by thread: Re: NSCalendar/NSDate - generating all months/days in a year
  • Index(es):
    • Date
    • Thread