Created
November 29, 2013 17:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could be wrong but i don't think [NSDate dateWithYear:year month:month day:1] is valid