//NSDate分类
@implementation NSDate (Extension)
//是否是七天内
- (BOOL)isWithinSevendays {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day < 7;
}
//是否为今年
- (BOOL)isThisYear
{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
return nowYear == selfYear;
}
//判断是不是本星期
- (BOOL)isThisWeak{
// 日历
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitWeekday | NSCalendarUnitMonth | NSCalendarUnitYear | kCFCalendarUnitDay | kCFCalendarUnitHour | kCFCalendarUnitMinute ;
//1.获得当前时间的 年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
NSDateComponents *sourceCmps = [calendar components:unit fromDate:self];
// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate:[NSDate date] toDate:self options:0];
NSInteger subDay = labs(dateCom.day);
NSInteger subMonth = labs(dateCom.month);
NSInteger subYear = labs(dateCom.year);
if (subYear == 0 && subMonth == 0) { //当相关的差值等于零的时候说明在一个年、月、日的时间范围内,不是按照零点到零点的时间算的
if (subDay > 6) { //相差天数大于6肯定不在一周内
return NO;
} else { //相差的天数大于或等于后面的时间所对应的weekday则不在一周内
if (dateCom.day >= 0 && dateCom.hour >=0 && dateCom.minute >= 0) { //比较的时间大于当前时间
//西方一周的开始是从周日开始算的,周日是1,周一是2,而我们是从周一开始算新的一周
NSInteger chinaWeekday = sourceCmps.weekday == 1 ? 7 : sourceCmps.weekday - 1;
if (subDay >= chinaWeekday) {
return NO;
} else {
return YES;
}
} else {
NSInteger chinaWeekday = sourceCmps.weekday == 1 ? 7 : nowCmps.weekday - 1;
if (subDay >= chinaWeekday) { //比较的时间比当前时间小,已经过去的时间
return NO;
} else {
return YES;
}
}
}
} else { //时间范围差值超过了一年或一个月的时间范围肯定就不在一个周内了
return NO;
}
}
//是否是今天
- (BOOL)isToday
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *nowString = [fmt stringFromDate:[NSDate date]];
NSString *selfString = [fmt stringFromDate:self];
return [nowString isEqualToString:selfString];
}
//是否是昨天
- (BOOL)isYesterday
{
// 日期格式化类
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 1;
}
//是否是前天
- (BOOL)iSThebeforeday {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSDate *nowDate = [fmt dateFromString:[fmt stringFromDate:[NSDate date]]];
NSDate *selfDate = [fmt dateFromString:[fmt stringFromDate:self]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmps = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 2;
}
//附
//当前时间区分上午/下午
- (NSDateComponents *)currentTime {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate* dt = [NSDate date];
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
NSDateComponents* comp = [gregorian components: unitFlags
fromDate:dt];
return comp;
}
//是否凌晨比较使用
- (NSDateComponents *)dateComponets
{
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
NSDateComponents* comp = [gregorian components: unitFlags
fromDate:self];
return comp;
}
@end