1 ## NSDateFormatter的作用
2 - NSString \* -> NSDate *
3
4 ```obj
5 - (nullable NSDate *)dateFromString:(NSString *)string;
6 ```
7
8 - NSDate \* -> NSString *
9
10 ```objc
11 - (NSString *)stringFromDate:(NSDate *)date;
12 ```
13
14 ## 常见的日期格式
15 - http://www.cnblogs.com/mailingfeng/archive/2011/07/28/2120422.html
16
17 ## NSString \* -> NSDate *
18 - 2015-11-20 09:10:05
19
20 ```objc
21 // 时间字符串
22 NSString *string = @"2015-11-20 09:10:05";
23
24 // 日期格式化类
25 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
26 // 设置日期格式(为了转换成功)
27 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
28
29 // NSString * -> NSDate *
30 NSDate *date = [fmt dateFromString:string];
31
32 NSLog(@"%@", date);
33 ```
34
35 - 11月-20号/2015年 09-10:05秒
36
37 ```objc
38 // 时间字符串
39 NSString *string = @"11月-20号/2015年 09-10:05秒";
40
41 // 日期格式化类
42 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
43 fmt.dateFormat = @"MM月-dd号/yyyy年 HH-mm:ss秒";
44
45 NSLog(@"%@", [fmt dateFromString:string]);
46 ```
47
48 - Tue May 31 17:46:55 +0800 2011
49
50 ```objc
51 // 时间字符串
52 NSString *string = @"Tue May 31 17:46:55 +0800 2011";
53
54 // 日期格式化类
55 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
56 fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
57 // fmt.dateFormat = @"EEE MMM dd HH:mm:ss ZZZZ yyyy";
58 // 设置语言区域(因为这种时间是欧美常用时间)
59 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
60
61 NSLog(@"%@", [fmt dateFromString:string]);
62 ```
63
64 - 1745645645645
65
66 ```objc
67 // 时间戳 : 从1970年1月1号 00:00:00开始走过的毫秒数
68
69 // 时间字符串 - 时间戳
70 NSString *string = @"1745645645645";
71 NSTimeInterval second = string.longLongValue / 1000.0;
72
73 // 时间戳 -> NSDate *
74 NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
75 NSLog(@"%@", date);
76 ```
77
78 ## NSCalendar的注意点
79 ```objc
80 #define iOS(version) ([UIDevice currentDevice].systemVersion.doubleValue >= (version))
81
82 NSCalendar *calendar = nil;
83 if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
84 calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
85 } else {
86 calendar = [NSCalendar currentCalendar];
87 }
88
89 NSCalendar *calendar = nil;
90 if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
91 calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
92 } else {
93 calendar = [NSCalendar currentCalendar];
94 }
95 ```
96
97 ## NSDate \* -> NSString *
98 ```objc
99 NSDate *date = [NSDate date];
100
101 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
102 fmt.dateFormat = @"yyyy年MM月dd号 HH:mm:ss";
103
104 NSString *string = [fmt stringFromDate:date];
105 ```
106
107 ## 获得日期元素
108 ```objc
109 NSString *string = @"2015-11-20 09:10:05";
110
111 NSString *month = [string substringWithRange:NSMakeRange(5, 2)];
112
113 NSLog(@"%@", month);
114 ```
115 ```objc
116 // 时间字符串
117 NSString *string = @"2015-11-20 09:10:05";
118
119 // 日期格式化类
120 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
121 // 设置日期格式(为了转换成功)
122 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
123
124 // NSString * -> NSDate *
125 NSDate *date = [fmt dateFromString:string];
126
127 // 利用NSCalendar处理日期
128 NSCalendar *calendar = [NSCalendar currentCalendar];
129 NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:date];
130 NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:date];
131 NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:date];
132
133 NSLog(@"%zd %zd %zd", month, hour, minute);
134 ```
135 ```objc
136 // 时间字符串
137 NSString *string = @"2015-11-20 09:10:05";
138
139 // 日期格式化类
140 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
141 // 设置日期格式(为了转换成功)
142 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
143
144 // NSString * -> NSDate *
145 NSDate *date = [fmt dateFromString:string];
146
147 // 利用NSCalendar处理日期
148 NSCalendar *calendar = [NSCalendar currentCalendar];
149
150 NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
151 NSDateComponents *cmps = [calendar components:unit fromDate:date];
152
153 // NSLog(@"%zd %zd %zd", cmps.year, cmps.month, cmps.day);
154 NSLog(@"%@", cmps);
155 ```
156
157 ## 日期比较
158 ```objc
159 // 时间字符串
160 NSString *createdAtString = @"2015-11-20 11:10:05";
161 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
162 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
163 NSDate *createdAtDate = [fmt dateFromString:createdAtString];
164
165 // 手机当前时间
166 NSDate *nowDate = [NSDate date];
167
168 /**
169 NSComparisonResult的取值
170 NSOrderedAscending = -1L, // 升序, 越往右边越大
171 NSOrderedSame, // 相等
172 NSOrderedDescending // 降序, 越往右边越小
173 */
174 // 获得比较结果(谁大谁小)
175 NSComparisonResult result = [nowDate compare:createdAtDate];
176 if (result == NSOrderedAscending) { // 升序, 越往右边越大
177 NSLog(@"createdAtDate > nowDate");
178 } else if (result == NSOrderedDescending) { // 降序, 越往右边越小
179 NSLog(@"createdAtDate < nowDate");
180 } else {
181 NSLog(@"createdAtDate == nowDate");
182 }
183 ```
184 ```objc
185 // 时间字符串
186 NSString *createdAtString = @"2015-11-20 09:10:05";
187 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
188 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
189 NSDate *createdAtDate = [fmt dateFromString:createdAtString];
190
191 // 手机当前时间
192 // NSDate *nowDate = [NSDate date];
193
194 // 获得createdAtDate和nowDate的时间间隔(间隔多少秒)
195 // NSTimeInterval interval = [nowDate timeIntervalSinceDate:createdAtDate];
196 NSTimeInterval interval = [createdAtDate timeIntervalSinceNow];
197 NSLog(@"%f", interval);
198 ```
199 ```objc
200 NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
201 fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
202
203 // 时间字符串
204 NSString *createdAtString = @"2015-11-01 09:10:05";
205 NSDate *createdAtDate = [fmt dateFromString:createdAtString];
206
207 // 其他时间
208 NSString *otherString = @"2015-10-31 08:56:45";
209 NSDate *otherDate = [fmt dateFromString:otherString];
210
211 // 获得NSCalendar
212 NSCalendar *calendar = nil;
213 if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
214 calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
215 } else {
216 calendar = [NSCalendar currentCalendar];
217 }
218
219 // 获得日期之间的间隔
220 NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
221 NSDateComponents *cmps = [calendar components:unit fromDate:createdAtDate toDate:otherDate options:0];
222
223 NSLog(@"%@", cmps);
224 ```
225
226 ## 条件判断的一些注意点
227 ```objc
228 1.判断一个数组中是否有具体内容
229 1> 正确
230 if (array.count) {
231
232 }
233
234 2> 错误
235 if (array) {
236
237 }
238
239 2.判断一个字符串是否有具体内容
240 1> 正确
241 if (string.length) {
242
243 }
244
245 2> 错误
246 if (string) {
247
248 }
249
250 ```