|
| 1 | +// |
| 2 | +// NSDate+TLCalendarExtention.m |
| 3 | +// OusiCanteen |
| 4 | +// |
| 5 | +// Created by 故乡的云 on 2018/8/20. |
| 6 | +// Copyright © 2018年 何青. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "NSDate+TLCalendarExtention.h" |
| 10 | + |
| 11 | +@implementation NSDate (TLCalendarExtention) |
| 12 | + |
| 13 | +// MARK: - 比较 |
| 14 | +- (NSDateComponents *)deltaFrom:(NSDate *)from { |
| 15 | + // 日历 |
| 16 | + NSCalendar *calendar = [NSCalendar currentCalendar]; |
| 17 | + |
| 18 | + // 比较时间 |
| 19 | + NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; |
| 20 | + |
| 21 | + return [calendar components:unit fromDate:from toDate:self options:0]; |
| 22 | +} |
| 23 | + |
| 24 | +- (NSTimeInterval)compareFrom:(NSDate *)from { |
| 25 | + NSTimeInterval time = [self timeIntervalSinceDate:from]; |
| 26 | + |
| 27 | + return time; |
| 28 | +} |
| 29 | + |
| 30 | +// MARK: - 判断 |
| 31 | + |
| 32 | +- (BOOL)isToday { |
| 33 | + return [[self curCalendar] isDateInToday:self]; |
| 34 | +} |
| 35 | + |
| 36 | +- (BOOL)isSameDate:(NSDate *)date { |
| 37 | + NSCalendar *calendar = [self curCalendar]; |
| 38 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; |
| 39 | + |
| 40 | + date = date ? date : [NSDate date]; |
| 41 | + NSDateComponents *nowCmps = [calendar components:unit fromDate:date]; |
| 42 | + NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; |
| 43 | + |
| 44 | + return nowCmps.year == selfCmps.year && nowCmps.month == selfCmps.month && nowCmps.day == selfCmps.day; |
| 45 | +} |
| 46 | + |
| 47 | +- (BOOL)isThisMonth:(NSDate *)date;{ |
| 48 | + NSCalendar *calendar = [self curCalendar]; |
| 49 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth; |
| 50 | + |
| 51 | + date = date ? date : [NSDate date]; |
| 52 | + NSDateComponents *nowCmps = [calendar components:unit fromDate:date]; |
| 53 | + NSDateComponents *selfCmps = [calendar components:unit fromDate:self]; |
| 54 | + |
| 55 | + return nowCmps.year == selfCmps.year && nowCmps.month == selfCmps.month; |
| 56 | +} |
| 57 | + |
| 58 | +- (BOOL)isThisYear:(NSDate *)date;{ |
| 59 | + NSCalendar *calendar = [self curCalendar]; |
| 60 | + |
| 61 | + date = date ? date : [NSDate date]; |
| 62 | + NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:date]; |
| 63 | + NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self]; |
| 64 | + |
| 65 | + return nowYear == selfYear; |
| 66 | +} |
| 67 | + |
| 68 | +- (BOOL)isYesterday{ |
| 69 | + return [[self curCalendar] isDateInYesterday:self]; |
| 70 | +} |
| 71 | + |
| 72 | +- (BOOL)isTomorrow{ |
| 73 | + return [[self curCalendar] isDateInTomorrow:self]; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +// MARK: - dateFormat(格式化) |
| 78 | ++ (NSDate *)dateWithYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day { |
| 79 | + NSString *dateStr = [NSString stringWithFormat:@"%zi-%zi-%zi", year, month, day]; |
| 80 | + NSDateFormatter *fmt = [NSDateFormatter new]; |
| 81 | + fmt.dateFormat = @"yyyy-MM-dd"; |
| 82 | + |
| 83 | + return [fmt dateFromString:dateStr]; |
| 84 | +} |
| 85 | + |
| 86 | ++ (NSDate *)dateWithFormatString:(NSString *)dateStr { |
| 87 | + NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; |
| 88 | + fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss"; |
| 89 | + return [fmt dateFromString:dateStr]; |
| 90 | +} |
| 91 | + |
| 92 | ++ (NSDate *)loaclDateWithFormatString:(NSString *)dateStr { |
| 93 | + return [[NSDate dateWithFormatString:dateStr] worldDateToLocalDate]; |
| 94 | +} |
| 95 | + |
| 96 | +- (NSString *)dateToFormatString:(NSString *)dateFormat { |
| 97 | + NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; |
| 98 | + fmt.dateFormat = dateFormat; |
| 99 | + return [fmt stringFromDate:self]; |
| 100 | +} |
| 101 | + |
| 102 | +// 世界时间转换为本地时间 |
| 103 | +- (NSDate *)worldDateToLocalDate |
| 104 | +{ |
| 105 | + //获取本地时区(中国时区) |
| 106 | + NSTimeZone* localTimeZone = [NSTimeZone localTimeZone]; |
| 107 | + //计算世界时间与本地时区的时间偏差值 |
| 108 | + NSInteger offset = [localTimeZone secondsFromGMTForDate:self]; |
| 109 | + //世界时间+偏差值 得出中国区时间 |
| 110 | + NSDate *localDate = [self dateByAddingTimeInterval:offset]; |
| 111 | + |
| 112 | + return localDate; |
| 113 | +} |
| 114 | + |
| 115 | +// MARK: - 取值 |
| 116 | +static NSCalendar *CALENDAR; |
| 117 | +- (NSCalendar *)curCalendar { |
| 118 | + if (CALENDAR == nil) { |
| 119 | + // 使用静态CALENDAR,防止高频创建影响性能 |
| 120 | + CALENDAR = [NSCalendar currentCalendar]; |
| 121 | + } |
| 122 | + return CALENDAR; |
| 123 | +} |
| 124 | + |
| 125 | +- (NSInteger)year { |
| 126 | + NSCalendar *calendar = [self curCalendar]; |
| 127 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; |
| 128 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 129 | + |
| 130 | + return cmps.year; |
| 131 | +} |
| 132 | + |
| 133 | +- (NSInteger)month { |
| 134 | + NSCalendar *calendar = [self curCalendar]; |
| 135 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; |
| 136 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 137 | + |
| 138 | + return cmps.month; |
| 139 | +} |
| 140 | + |
| 141 | +- (NSInteger)day { |
| 142 | + NSCalendar *calendar = [self curCalendar]; |
| 143 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; |
| 144 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 145 | + |
| 146 | + return cmps.day; |
| 147 | +} |
| 148 | + |
| 149 | +- (NSInteger)hour { |
| 150 | + NSCalendar *calendar = [self curCalendar]; |
| 151 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | |
| 152 | + NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; |
| 153 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 154 | + |
| 155 | + return cmps.hour; |
| 156 | +} |
| 157 | + |
| 158 | +- (NSInteger)minute { |
| 159 | + NSCalendar *calendar = [self curCalendar]; |
| 160 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | |
| 161 | + NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; |
| 162 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 163 | + |
| 164 | + return cmps.minute; |
| 165 | +} |
| 166 | + |
| 167 | +- (NSInteger)second { |
| 168 | + NSCalendar *calendar = [self curCalendar]; |
| 169 | + NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | |
| 170 | + NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; |
| 171 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 172 | + |
| 173 | + return cmps.second; |
| 174 | +} |
| 175 | + |
| 176 | +- (NSInteger)weekday { |
| 177 | + NSCalendar *calendar = [self curCalendar]; |
| 178 | + NSCalendarUnit unit = NSCalendarUnitWeekday; |
| 179 | + NSDateComponents *cmps = [calendar components:unit fromDate:self]; |
| 180 | + |
| 181 | + return cmps.weekday; |
| 182 | +} |
| 183 | + |
| 184 | +- (NSInteger)startWeekOfMonth { |
| 185 | + NSDate *date = [NSDate dateWithYear:self.year month:self.month day:1]; |
| 186 | + |
| 187 | + return date.weekday; |
| 188 | +} |
| 189 | + |
| 190 | +- (NSInteger)countOfMonth { |
| 191 | + NSCalendar *calendar = [self curCalendar]; |
| 192 | + NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate:self]; |
| 193 | + |
| 194 | + return range.length; |
| 195 | +} |
| 196 | + |
| 197 | +@end |
0 commit comments