Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of months and years between two dates

I would like to fetch name of months and years between two dates. Suppose my start date is 01-23-2010 and end date is 02-25-2011 hence I would like to get list of months with associated year e.g January 2010, February 2010----- February 2011.

like image 621
sandy Avatar asked Dec 22 '22 17:12

sandy


2 Answers

Here's approximately how I'd do it (warning: type in a browser, yadda yadda):

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);

That should give you an array of NSDate objects representing dates that occur approximately one month apart, starting at your start date and ending at or around your end date.

If you want to display them in a readable fashion, you'll use an NSDateFormatter:

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"MMMM yyyy"];
for (NSDate *date in dates) {
  NSLog(@"%@", [f stringFromDate:date]);
}

[f release];

When I run this on my computer, I get:

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

This may seem kinda complex, but it has a couple advantages:

  • it will work regardless of what calendaring system you're using. Just change the [NSCalendar currentCalendar] call to be a different calendar and it'll work in that one (Hebrew, Islamic, etc)
  • it accounts for months with bizarre numbers of days (28 days vs 29 days, or calendars with leap months [yes they exist])
like image 162
Dave DeLong Avatar answered Jan 04 '23 22:01

Dave DeLong


What you want to use is NSDateFormatter. The documentation says:

Instances of NSDateFormatter create string representations of NSDate (and NSCalendarDate) objects, and convert textual representations of dates and times into NSDate objects. You can express the representation of dates and times flexibly using pre-set format styles or custom format strings.

If you need to determine what the months actually are between two dates, you can use NSCalendar.

like image 33
ericg Avatar answered Jan 04 '23 23:01

ericg