#import <Foundation/Foundation.h>
@interface NSCalendar (CqExtension)
+ (instancetype)Cq_calendar;
@end
---------------------------------------------------
#import "NSCalendar+CqExtension.h"
@implementation NSCalendar (CqExtension)
+ (instancetype)Cq_calendar{
// 判断NSCalendar这个类是否有calendarWithIdentifier:这个[类方法],类方法判断是否存在方法可以用于版本的判断
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
}else{
return [NSCalendar currentCalendar];
}
}
#import <Foundation/Foundation.h>
@interface CqDateItem : NSObject
/** 天 */
@property (nonatomic,assign) NSInteger day;
/** 时 */
@property (nonatomic,assign) NSInteger hour;
/** 分 */
@property (nonatomic,assign) NSInteger minute;
/** 秒 */
@property (nonatomic,assign) NSInteger second;
@end
@interface NSDate (CqExtension)
- (CqDateItem *)Cq_timeIntervalSinceDate:(NSDate *)anotherDate;
//是否当前天
- (BOOL)Cq_isToday;
//是否昨天
- (BOOL)Cq_isYesterday;
//是否当前天
- (BOOL)Cq_isTomorrow;
//是否今年
- (BOOL)Cq_isThisYear;
@end
---------------------------------------------------
#import "NSDate+CqExtension.h"
#import "NSCalendar+CqExtension.h"
@implementation CqDateItem
- (NSString *)description{
return [NSString stringWithFormat:@"%zd天%zd时%zd分%zd秒",self.day,self.hour,self.minute,self.second];
}
@end
@implementation NSDate (CqExtension)
- (CqDateItem *)Cq_timeIntervalSinceDate:(NSDate *)anotherDate{
//创建时间createAtDate 和 nowDate的时间间隔
NSTimeInterval interval = [self timeIntervalSinceDate:anotherDate];
CqDateItem *item = [[CqDateItem alloc] init];
//相差多少天
int intInterval = (int)interval;
int secondPerDay = 24 * 3600;
item.day = intInterval / secondPerDay;
//相差几小时
int secondPerHour = 3600;
item.hour = (intInterval % secondPerDay) / secondPerHour;
//相差几分钟
int secondPerMinute = 60;
item.minute = ((intInterval % secondPerDay) % secondPerHour) / secondPerMinute;
//相差几秒
item.second = ((intInterval % secondPerDay) % secondPerHour) % secondPerMinute;
return item;
}
- (BOOL)Cq_isToday{
//判断self这日期是否是今天
NSCalendar *calendar = [NSCalendar Cq_calendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *selfCmp = [calendar components:unit fromDate:self];
NSDateComponents *nowCmp = [calendar components:unit fromDate:[NSDate date]];
return [selfCmp isEqual:nowCmp];
}
- (BOOL)Cq_isYesterday{
// self 2015-12-09 22:10:01 -> 2015-12-09 00:00:00
// now 2015-12-10 12:10:01 -> 2015-12-01 00:00:00
// 昨天:0year 0month 1day 0hour 0minute 0second
// NSDate * -> NSString * -> NSDate *
//判断这日期是否昨天
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyyMMdd";
//生成只有年月日的字符串对象
NSString *selfString = [fmt stringFromDate:self];
NSString *nowString = [fmt stringFromDate:[NSDate date]];
//生成只有年月日的日期对象
NSDate *selfDate = [fmt dateFromString:selfString];
NSDate *nowDate = [fmt dateFromString:nowString];
NSCalendar *calendar = [NSCalendar Cq_calendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == 1;
}
- (BOOL)Cq_isTomorrow{
//判断这日期是否昨天
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyyMMdd";
//生成只有年月日的字符串对象
NSString *selfString = [fmt stringFromDate:self];
NSString *nowString = [fmt stringFromDate:[NSDate date]];
//生成只有年月日的日期对象
NSDate *selfDate = [fmt dateFromString:selfString];
NSDate *nowDate = [fmt dateFromString:nowString];
NSCalendar *calendar = [NSCalendar Cq_calendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *cmps = [calendar components:unit fromDate:selfDate toDate:nowDate options:0];
return cmps.year == 0
&& cmps.month == 0
&& cmps.day == -1;
}
- (BOOL)Cq_isThisYear{
//判断self的日期是否今年
NSCalendar *calendar = [NSCalendar Cq_calendar];
NSInteger selfYear = [calendar components:NSCalendarUnitYear fromDate:self].year;
NSInteger nowYear = [calendar components:NSCalendarUnitYear fromDate:[NSDate date]].year;
return selfYear == nowYear;
}