-
Notifications
You must be signed in to change notification settings - Fork 8
/
NSDate-Calendar.m
105 lines (86 loc) · 3.61 KB
/
NSDate-Calendar.m
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
//
// NSDate-Calendar.m
// Motivator
//
// Created by Jon Maddox on 11/26/08.
//
//
// NSCalendarDate does not exist in CocoaTouch on the iPhone. So I made this category to put
// a little of the support I needed from NSCalendarDate into NSDate
//
//
// The MIT License
//
// Copyright (c) 2008 Mustache, Inc
//
// 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-Calendar.h"
@implementation NSDate(Calendar)
+ (id)today{
NSDate *theDate = [NSDate date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:theDate];
[comps setYear:[theDate year]];
[comps setMonth:[theDate month]];
[comps setDay:[theDate day]];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
- (int)year{
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit) fromDate:self];
return [comps year];
}
- (int)month{
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSMonthCalendarUnit) fromDate:self];
return [comps month];
}
- (int)day{
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:self];
return [comps day];
}
- (int)weekday{
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSWeekdayCalendarUnit) fromDate:self];
return [comps weekday];
}
- (NSDate *)firstDayOfCurrentMonth{
NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
[comps setDay:1];
return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
- (NSDate *)firstDayOfCurrentWeek{
NSDateComponents *adjustmentComps = [[NSDateComponents alloc] init];
[adjustmentComps setDay:-([self weekday]-1)];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:adjustmentComps toDate:self options:0];
[adjustmentComps release];
return newDate;
}
- (NSDate *)dateByAddingYears:(int)years months:(int)months days:(int)days hours:(int)hours minutes:(int)minutes seconds:(int)seconds{
NSDateComponents *adjustmentComps = [[NSDateComponents alloc] init];
[adjustmentComps setYear:years];
[adjustmentComps setMonth:months];
[adjustmentComps setDay:days];
[adjustmentComps setHour:hours];
[adjustmentComps setMinute:minutes];
[adjustmentComps setSecond:seconds];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:adjustmentComps toDate:self options:0];
[adjustmentComps release];
return newDate;
}
@end