-1

So i'm looking to create an object for each month in-between two dates..

Start: 01/08/2012

End: 02/10/2012

would be two months so id like to list out:

01/09/2012 01/10/2012

..ready for me to create an NSDate for each of those to push to Parse(.com)..

Here's what i've got so far:

    NSString *start = @"2010-09-15";
    NSString *end = @"2011-07-15";

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [f dateFromString:start];
    NSDate *endDate = [f dateFromString:end];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSMonthCalendarUnit
                                                        fromDate:startDate
                                                          toDate:endDate
                                                         options:0];

    int i=0;

    for (i = 0; i < [components month]; i++)
    {

        NSLog(@"%i", i+1);

        NSDateFormatter *df = [NSDateFormatter new];
        // change locale if the standard is not what you want
        NSArray *monthNames = [df standaloneMonthSymbols];
        NSString *monthName = [monthNames objectAtIndex:(i)];

        NSLog(@"Month name: %@", monthName);

    }

Thanks for your help!

1 Answers1

0

You have the right idea. You have code to convert the start date string to an NSDate, and to figure out the number of months between the 2 dates. Presumably what you want to send to parse.com is date strings in the same format, right?

Let's call the number of months between your start date and end date monthCount.

In that case, in your for loop, you should use the NSCalendar method dateByAddingComponents:toDate:options: to add 0 -> monthCount to the date, and then use your date formatter's stringFromDate method to convert that date to a date string.

Duncan C
  • 128,072
  • 22
  • 173
  • 272