Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 285 lines (229 sloc) 10.157 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
//
// NSDateAdditions.m
// Created by Devin Ross on 7/28/09.
//
/*
tapku || http://github.com/devinross/tapkulibrary
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "NSDate+TKCategory.h"

#pragma mark - NSDate + TKCategory
@implementation NSDate (TKCategory)

#pragma mark Yesterday
+ (NSDate*) yesterday{
return [NSDate yesterdayWithTimeZone:[NSTimeZone defaultTimeZone]];
}
+ (NSDate*) yesterdayWithTimeZone:(NSTimeZone*)timeZone{
NSDateComponents *comp = [[NSDate date] dateComponentsWithTimeZone:timeZone];
comp.day--;
return [NSDate dateWithDateComponents:comp];
}


#pragma mark Tomorrow
+ (NSDate*) tomorrow{
return [NSDate tomorrowWithTimeZone:[NSTimeZone defaultTimeZone]];
}
+ (NSDate*) tomorrowWithTimeZone:(NSTimeZone*)timeZone{
NSDateComponents *comp = [[NSDate date] dateComponentsWithTimeZone:timeZone];
comp.day++;
return [NSDate dateWithDateComponents:comp];
}

#pragma mark Month
+ (NSDate*) month{
    return [[NSDate date] monthDateWithTimeZone:[NSTimeZone defaultTimeZone]];
}
+ (NSDate*) monthWithTimeZone:(NSTimeZone*)timeZone{
    return [[NSDate date] monthDateWithTimeZone:timeZone];
}
- (NSDate*) monthDate{
return [self monthDateWithTimeZone:[NSTimeZone defaultTimeZone]];
}
- (NSDate*) monthDateWithTimeZone:(NSTimeZone*)timeZone{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = timeZone;
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:self];
[comp setDay:1];
NSDate *date = [gregorian dateFromComponents:comp];
    return date;
}


#pragma mark Between
- (NSInteger) monthsBetweenDate:(NSDate *)toDate{
return [self monthsBetweenDate:toDate timeZone:[NSTimeZone defaultTimeZone]];
}
- (NSInteger) monthsBetweenDate:(NSDate *)toDate timeZone:(NSTimeZone*)timeZone{
if([self compare:toDate]==NSOrderedSame) return 0;

NSDate *first = nil, *last = nil;
if([self compare:toDate] == NSOrderedAscending){
first = self;
last = toDate;
}else{
first = toDate;
last = self;
}

NSDateComponents *d1 = [first dateComponentsWithTimeZone:timeZone];
NSDateComponents *d2 = [last dateComponentsWithTimeZone:timeZone];

if(d1.year == d2.year)
return d2.month - d1.month;


NSInteger ret = 12 - d1.month;
ret += d2.month;
d1.year += 1;
ret += 12 * (d2.year-d1.year);

return ret;
}
- (NSInteger) daysBetweenDate:(NSDate*)date {
    NSTimeInterval time = [self timeIntervalSinceDate:date];
    return ((abs(time) / (60.0 * 60.0 * 24.0)) + 0.5);
}

#pragma mark Same Day
- (BOOL) isSameDay:(NSDate*)anotherDate{
return [self isSameDay:anotherDate timeZone:[NSTimeZone defaultTimeZone]];
}
- (BOOL) isSameDay:(NSDate*)anotherDate timeZone:(NSTimeZone*)timeZone{
NSCalendar* calendar = [NSCalendar currentCalendar];
calendar.timeZone = timeZone;
NSDateComponents* components1 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
NSDateComponents* components2 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anotherDate];
return ([components1 year] == [components2 year] && [components1 month] == [components2 month] && [components1 day] == [components2 day]);
}

#pragma mark Same Month
- (BOOL) isSameMonth:(NSDate *)anotherDate{
return [self isSameMonth:anotherDate timeZone:[NSTimeZone defaultTimeZone]];
}
- (BOOL) isSameMonth:(NSDate *)anotherDate timeZone:(NSTimeZone *)timeZone{

NSCalendar* calendar = [NSCalendar currentCalendar];
calendar.timeZone = timeZone;
NSDateComponents* components1 = [calendar components:NSYearCalendarUnit fromDate:self];
NSDateComponents* components2 = [calendar components:NSYearCalendarUnit fromDate:anotherDate];
return components1.year == components2.year && components1.month == components2.month;
}

#pragma mark Same Year
- (BOOL) isSameYear:(NSDate *)anotherDate{
return [self isSameYear:anotherDate timeZone:[NSTimeZone defaultTimeZone]];
}
- (BOOL) isSameYear:(NSDate *)anotherDate timeZone:(NSTimeZone *)timeZone{

NSCalendar* calendar = [NSCalendar currentCalendar];
calendar.timeZone = timeZone;
NSDateComponents* components1 = [calendar components:NSYearCalendarUnit fromDate:self];
NSDateComponents* components2 = [calendar components:NSYearCalendarUnit fromDate:anotherDate];
return components1.year == components2.year;

}

#pragma mark Is Today
- (BOOL) isToday{
return [self isSameDay:[NSDate date]];
}
- (BOOL) isTodayWithTimeZone:(NSTimeZone*)timeZone{
return [self isSameDay:[NSDate date] timeZone:timeZone];
}

- (BOOL) isTomorrow{
return [self isTomorrowWithTimeZone:[NSTimeZone defaultTimeZone]];
}
- (BOOL) isTomorrowWithTimeZone:(NSTimeZone*)timeZone{
NSDateComponents *comp = [[NSDate date] dateComponentsWithTimeZone:timeZone];
comp.day++;
NSDate *actualTomorrow = [NSDate dateWithDateComponents:comp];
return [self isSameDay:actualTomorrow timeZone:timeZone];
}


- (BOOL) isYesterday{
return [self isYesterdayWithTimeZone:[NSTimeZone defaultTimeZone]];
}
- (BOOL) isYesterdayWithTimeZone:(NSTimeZone*)timeZone{
NSDateComponents *comp = [[NSDate date] dateComponentsWithTimeZone:timeZone];
comp.day--;
NSDate *actualTomorrow = [NSDate dateWithDateComponents:comp];
return [self isSameDay:actualTomorrow timeZone:timeZone];
}

#pragma mark Month & Year String
- (NSString *) monthYearString{
return [self monthYearStringWithTimeZone:[NSTimeZone defaultTimeZone]];
}
- (NSString *) monthYearStringWithTimeZone:(NSTimeZone*)timeZone{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"yMMMM"
options:0
locale:[NSLocale currentLocale]];
return [dateFormatter stringFromDate:self];
}

- (NSString*) monthString{
return [self monthStringWithTimeZone:[NSTimeZone defaultTimeZone]];
}
- (NSString*) monthStringWithTimeZone:(NSTimeZone*)timeZone{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = timeZone;
[dateFormatter setDateFormat:@"MMMM"];
return [dateFormatter stringFromDate:self];
}

- (NSString*) yearString{
return [self yearStringWithTimeZone:[NSTimeZone defaultTimeZone]];
}
- (NSString*) yearStringWithTimeZone:(NSTimeZone*)timeZone{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = timeZone;
[dateFormatter setDateFormat:@"yyyy"];
return [dateFormatter stringFromDate:self];
}


#pragma mark Date Compontents
- (NSDateComponents*) dateComponentsWithTimeZone:(NSTimeZone*)timeZone{

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = timeZone;
return [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSTimeZoneCalendarUnit) fromDate:self];
}
+ (NSDate*) dateWithDateComponents:(NSDateComponents*)components{

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = components.timeZone;
return [gregorian dateFromComponents:components];
}


- (NSDate *) dateByAddingDays:(NSUInteger)days {
NSDateComponents *c = [[NSDateComponents alloc] init];
c.day = days;
return [[NSCalendar currentCalendar] dateByAddingComponents:c toDate:self options:0];
}
+ (NSDate *) dateWithDatePart:(NSDate *)aDate andTimePart:(NSDate *)aTime {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSString *datePortion = [dateFormatter stringFromDate:aDate];

[dateFormatter setDateFormat:@"HH:mm"];
NSString *timePortion = [dateFormatter stringFromDate:aTime];

[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
NSString *dateTime = [NSString stringWithFormat:@"%@ %@",datePortion,timePortion];
return [dateFormatter dateFromString:dateTime];
}



- (NSDate*) firstDateOfWeekWithTimeZone:(NSTimeZone*)timeZone{
NSCalendar *gregorian = [NSCalendar currentCalendar];

NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:self];
weekdayComponents.timeZone = timeZone;

NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
componentsToSubtract.timeZone = timeZone;

[componentsToSubtract setDay: - ([weekdayComponents weekday] - [gregorian firstWeekday])];

NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:self options:0];


NSDateComponents *components = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: beginningOfWeek];
components.timeZone = timeZone;

beginningOfWeek = [gregorian dateFromComponents: components];

return beginningOfWeek;
}


- (NSDate*) firstDateOfWeek{
return [self firstDateOfWeekWithTimeZone:[NSTimeZone defaultTimeZone]];
}


+ (NSDate*) firstDateOfWeekWithTimeZone:(NSTimeZone*)timeZone{
return [[NSDate date] firstDateOfWeekWithTimeZone:timeZone];
}
+ (NSDate*) firstDateOfWeek{
return [[NSDate date] firstDateOfWeek];
}

@end
Something went wrong with that request. Please try again.