Created
October 26, 2013 10:32
-
-
Save sebk/7167868 to your computer and use it in GitHub Desktop.
NSDate methods
This file contains 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
@interface NSDate (Additions) | |
-(int)numDaysInMonth; | |
-(int)firstWeekDayInMonth; | |
-(NSArray *)weeksOfMonth; | |
-(NSDate*)offsetDay:(int)days; | |
-(NSDate *)offsetMonth:(int)numMonths; | |
-(BOOL)isWeekend; | |
-(int)day; | |
-(int)month; | |
-(int)year; | |
-(int)quarter; | |
-(NSString*)weekdayName; | |
-(BOOL)isToday; | |
-(BOOL)isMonday; | |
@end | |
#import "NSDate+Additions.h" | |
@implementation NSDate (Additions) | |
-(int)numDaysInMonth { | |
NSCalendar *cal = [NSCalendar currentCalendar]; | |
NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self]; | |
NSUInteger numberOfDaysInMonth = rng.length; | |
return numberOfDaysInMonth; | |
} | |
-(int)firstWeekDayInMonth { | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSGregorianCalendar]; | |
[gregorian setFirstWeekday:2]; //monday is first day | |
//Set date to first of month | |
NSDateComponents *comps = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:self]; | |
[comps setDay:1]; | |
NSDate *newDate = [gregorian dateFromComponents:comps]; | |
return [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:newDate]; | |
} | |
-(NSDate *)offsetMonth:(int)numMonths { | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSGregorianCalendar]; | |
[gregorian setFirstWeekday:2]; //monday is first day | |
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; | |
[offsetComponents setMonth:numMonths]; | |
return [gregorian dateByAddingComponents:offsetComponents | |
toDate:self options:0]; | |
} | |
-(NSDate*)offsetDay:(int)days { | |
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; | |
[offsetComponents setDay:days]; | |
return [gregorian dateByAddingComponents:offsetComponents toDate:self options:0]; | |
} | |
-(int)day { | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateComponents *components = [gregorian components:NSDayCalendarUnit fromDate:self]; | |
return [components day]; | |
} | |
-(int)year { | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:self]; | |
return [components year]; | |
} | |
-(int)month { | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateComponents *components = [gregorian components:NSMonthCalendarUnit fromDate:self]; | |
return [components month]; | |
} | |
-(int)quarter { | |
/* | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateComponents *components = [gregorian components:NSQuarterCalendarUnit fromDate:self]; | |
return [components quarter]; | |
*/ | |
NSDateFormatter *quarterOnly = [[VWKKCentralStorage sharedInstance] dateFormatter];//[[NSDateFormatter alloc]init]; | |
[quarterOnly setDateFormat:@"Q"]; | |
int quarter = [[quarterOnly stringFromDate:self] intValue]; | |
return quarter; | |
} | |
- (NSArray *)weeksOfMonth { | |
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; | |
NSDateComponents *components = [[NSDateComponents alloc] init]; | |
[components setMonth:[self month]]; | |
[components setYear:[self year]]; | |
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit | |
inUnit:NSMonthCalendarUnit | |
forDate:[calendar dateFromComponents:components]]; | |
NSDateFormatter *dateFormatter = [[VWKKCentralStorage sharedInstance] dateFormatter];//[[NSDateFormatter alloc]init]; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; | |
NSMutableSet *weeks = [[NSMutableSet alloc] init ]; | |
for(int i = 0; i < range.length; i++) | |
{ | |
NSString *temp = [NSString stringWithFormat:@"%4d-%2d-%2d", [self year], [self month], range.location+i]; | |
NSDate *date = [dateFormatter dateFromString:temp ]; | |
int week = [[calendar components: NSWeekOfYearCalendarUnit fromDate:date] weekOfYear]; | |
[weeks addObject:[NSNumber numberWithInt:week]]; | |
} | |
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]; | |
return [weeks sortedArrayUsingDescriptors:@[descriptor]]; | |
} | |
-(BOOL)isWeekend { | |
int dayOfWeek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:self] weekday]; | |
if (dayOfWeek == 7 || dayOfWeek == 1) { | |
return YES; | |
} | |
return NO; | |
} | |
-(BOOL)isMonday { | |
int dayOfWeek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:self] weekday]; | |
if (dayOfWeek == 2) { | |
return YES; | |
} | |
return NO; | |
} | |
-(NSString*)weekdayName { | |
NSDateFormatter *dateFormatter = [[VWKKCentralStorage sharedInstance] dateFormatter];//[[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"EEE"]; | |
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[NSLocale preferredLanguages][0] ] ]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
-(BOOL)isToday { | |
NSDate *date = [NSDate date]; | |
if ([date day] == [self day] && [date month] == [self month] && [date year] == [self year] ) { | |
return YES; | |
} | |
return NO; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment