Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's approximately how I'd do it (warning: type in a browser, yadda yadda):</p> <pre><code>NSDate *startDate = ...; // your start date NSDate *endDate = ...; // your end date NSDateComponents *monthDifference = [[NSDateComponents alloc] init]; NSMutableArray *dates = [NSMutableArray arrayWithObject:startDate]; NSUInteger monthOffset = 0; NSDate *nextDate = startDate; do { [dates addObject:nextDate]; [monthDifference setMonth:monthOffset++]; NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:monthDifference toDate:startDate options:0]; nextDate = d; } while([nextDate compare:endDate] == NSOrderedAscending); </code></pre> <p>That should give you an array of <code>NSDate</code> objects representing dates that occur approximately one month apart, starting at your start date and ending at or around your end date.</p> <p>If you want to display them in a readable fashion, you'll use an <code>NSDateFormatter</code>:</p> <pre><code>NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@"MMMM yyyy"]; for (NSDate *date in dates) { NSLog(@"%@", [f stringFromDate:date]); } [f release]; </code></pre> <hr> <p>When I run this on my computer, I get:</p> <pre><code>EmptyFoundation[3327:a0f] January 2010 EmptyFoundation[3327:a0f] February 2010 EmptyFoundation[3327:a0f] March 2010 EmptyFoundation[3327:a0f] April 2010 EmptyFoundation[3327:a0f] May 2010 EmptyFoundation[3327:a0f] June 2010 EmptyFoundation[3327:a0f] July 2010 EmptyFoundation[3327:a0f] August 2010 EmptyFoundation[3327:a0f] September 2010 EmptyFoundation[3327:a0f] October 2010 EmptyFoundation[3327:a0f] November 2010 EmptyFoundation[3327:a0f] December 2010 EmptyFoundation[3327:a0f] January 2011 EmptyFoundation[3327:a0f] February 2011 </code></pre> <p>This may seem kinda complex, but it has a couple advantages:</p> <ul> <li>it will work <em>regardless</em> of what calendaring system you're using. Just change the <code>[NSCalendar currentCalendar]</code> call to be a different calendar and it'll work in that one (Hebrew, Islamic, etc)</li> <li>it accounts for months with bizarre numbers of days (28 days vs 29 days, or calendars with leap months [yes they exist])</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload