/**
* 是否为今天
*/
- (BOOL)isToday
{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
// 1.获得当前时间的年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// 2.获得self的年月日
NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
return
(selfCmps.year == nowCmps.year) &&
(selfCmps.month == nowCmps.month) &&
(selfCmps.day == nowCmps.day);
}
/**
* 是否为昨天
*/
#warning 待补充
- (BOOL)isYesterday
{
NSDate *now = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = @"yyyy-MM-dd";
NSString *nowString = [df stringFromDate:now];
NSDate *nowDate = [df dateFromString:nowString];
NSString *selfString = [df stringFromDate:self];
NSDate *selfDate = [df dateFromString:selfString];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *datec = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
return datec.day == 1;
}
/**
* 是否为今年
*/
- (BOOL)isThisYear
{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitYear;
// 1.获得当前时间的年月日
NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
// 2.获得self的年月日
NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
return nowCmps.year == selfCmps.year;
}
- (NSDateComponents *)deltaWithNow
{
NSCalendar *calendar = [NSCalendar currentCalendar];
int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
}