You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

145 lines
5.8 KiB

//
// NSDate+convenience.m
//
// Created by in 't Veen Tjeerd on 4/23/12.
// Copyright (c) 2012 Vurig Media. All rights reserved.
//
#import "NSDate+convenience.h"
@implementation NSDate (Convenience)
-(int)year {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:self];
return [components year];
}
-(int)month {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSMonthCalendarUnit fromDate:self];
return [components month];
}
-(int)day {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSDayCalendarUnit fromDate:self];
return [components day];
}
-(int)firstWeekDayInMonth {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregorian setFirstWeekday:2]; //monday is first day
//[gregorian setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"]];
//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:NSCalendarIdentifierGregorian];
[gregorian setFirstWeekday:2]; //monday is first day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setMonth:numMonths];
//[offsetComponents setHour:1];
//[offsetComponents setMinute:30];
return [gregorian dateByAddingComponents:offsetComponents
toDate:self options:0];
}
-(NSDate *)offsetHours:(int)hours {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregorian setFirstWeekday:2]; //monday is first day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
//[offsetComponents setMonth:numMonths];
[offsetComponents setHour:hours];
return [gregorian dateByAddingComponents:offsetComponents
toDate:self options:0];
}
-(NSDate *)offsetDay:(int)numDays {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregorian setFirstWeekday:2]; //monday is first day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:numDays];
//[offsetComponents setHour:1];
//[offsetComponents setMinute:30];
return [gregorian dateByAddingComponents:offsetComponents
toDate:self options:0];
}
-(int)numDaysInMonth {
NSCalendar *cal = [NSCalendar currentCalendar];
NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self];
NSUInteger numberOfDaysInMonth = rng.length;
return numberOfDaysInMonth;
}
+(NSDate *)dateStartOfDay:(NSDate *)date {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components =
[gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit) fromDate: date];
return [gregorian dateFromComponents:components];
}
+(NSDate *)dateStartOfWeek {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregorian setFirstWeekday:2]; //monday is first day
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: - ((([components weekday] - [gregorian firstWeekday])
+ 7 ) % 7)];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:[NSDate date] options:0];
NSDateComponents *componentsStripped = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate: beginningOfWeek];
//gestript
beginningOfWeek = [gregorian dateFromComponents: componentsStripped];
return beginningOfWeek;
}
+(NSDate *)dateEndOfWeek {
NSCalendar *gregorian =[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
[componentsToAdd setDay: + (((([components weekday] - [gregorian firstWeekday])
+ 7 ) % 7))+6];
NSDate *endOfWeek = [gregorian dateByAddingComponents:componentsToAdd toDate:[NSDate date] options:0];
NSDateComponents *componentsStripped = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate: endOfWeek];
//gestript
endOfWeek = [gregorian dateFromComponents: componentsStripped];
return endOfWeek;
}
@end