Skip to content

Instantly share code, notes, and snippets.

@ariok
Created November 29, 2013 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariok/7708975 to your computer and use it in GitHub Desktop.
Save ariok/7708975 to your computer and use it in GitHub Desktop.
Get the NSDate representations of the first and last days of the given month in the specified year
// Return an array:
// Index 0: NSDate for the First day of the month
// Index 1: NSDate for the Last day of the month
+ (NSArray *)dateRangeForYear:(NSInteger)year Month:(NSInteger)month{
// Build calendar and date components
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* dateComps = [[NSDateComponents alloc] init];
// Set the month
[dateComps setMonth:month];
// Get the range
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[calendar dateFromComponents:dateComps]];
NSDate *fromDate = [NSDate dateWithYear:year month:month day:1];
NSDate *toDate = [NSDate dateWithYear:year month:month day:range.length];
return @[fromDate, toDate];
}
@MrJDHog
Copy link

MrJDHog commented Apr 3, 2014

Could be wrong but i don't think [NSDate dateWithYear:year month:month day:1] is valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment