CBHDateKit
CBHDateKit
利用现有的 NSDate
API 添加了诸如相对日期创建、许多常用检查以及分解日期的便捷方法等功能。此外,还扩展了 NSDateFormatter
API,以提供便捷的工厂以及 RFC 822 和 RFC 3339 的预构建工厂。
NSDate
#pragma mark - Shared Calendar
+ (NSCalendar *)sharedCalendar;
#pragma mark - Relative Dates
+ (instancetype)dateTomorrow;
+ (instancetype)dateYesterday;
+ (instancetype)dateWithMinutesFromNow:(NSInteger)minutes;
+ (instancetype)dateWithMinutesBeforeNow:(NSInteger)minutes;
+ (instancetype)dateWithHoursFromNow:(NSInteger)hours;
+ (instancetype)dateWithHoursBeforeNow:(NSInteger)hours;
+ (instancetype)dateWithDaysFromNow:(NSInteger)days;
+ (instancetype)dateWithDaysBeforeNow:(NSInteger)days;
#pragma mark - Comparing Dates
- (BOOL)isEqualToDateIgnoringTime:(NSDate *)date;
- (BOOL)isEqualToDateIgnoringNanoSeconds:(NSDate *)date;
- (BOOL)isSameDayAsDate:(NSDate *)date;
@property (nonatomic, readonly) BOOL isToday;
@property (nonatomic, readonly) BOOL isTomorrow;
@property (nonatomic, readonly) BOOL isYesterday;
- (BOOL)isSameWeekAsDate:(NSDate *)date;
@property (nonatomic, readonly) BOOL isThisWeek;
@property (nonatomic, readonly) BOOL isNextWeek;
@property (nonatomic, readonly) BOOL isLastWeek;
- (BOOL)isSameMonthAsDate:(NSDate *)date;
@property (nonatomic, readonly) BOOL isThisMonth;
- (BOOL)isSameYearAsDate:(NSDate *)date;
@property (nonatomic, readonly) BOOL isThisYear;
@property (nonatomic, readonly) BOOL isNextYear;
@property (nonatomic, readonly) BOOL isLastYear;
- (BOOL)isEarlierThanDate:(NSDate *)date;
- (BOOL)isLaterThanDate:(NSDate *)date;
@property (nonatomic, readonly) BOOL isInTheFuture;
@property (nonatomic, readonly) BOOL isInThePast;
#pragma mark - Date Roles
@property (nonatomic, readonly) BOOL isWorkday;
@property (nonatomic, readonly) BOOL isWeekend;
#pragma mark - Adjusting Dates
- (NSDate *)dateByAddingMinutes:(NSInteger)minutes;
- (NSDate *)dateBySubtractingMinutes:(NSInteger)minutes;
- (NSDate *)dateByAddingHours:(NSInteger)hours;
- (NSDate *)dateBySubtractingHours:(NSInteger)hours;
- (NSDate *)dateByAddingDays:(NSInteger)days;
- (NSDate *)dateBySubtractingDays:(NSInteger)days;
@property (nonatomic, readonly) NSDate *dateAtStartOfDay;
#pragma mark - Intervals
- (NSInteger)secondsAfterDate:(NSDate *)date;
- (NSInteger)secondsBeforeDate:(NSDate *)date;
- (NSInteger)minutesAfterDate:(NSDate *)date;
- (NSInteger)minutesBeforeDate:(NSDate *)date;
- (NSInteger)hoursAfterDate:(NSDate *)date;
- (NSInteger)hoursBeforeDate:(NSDate *)date;
- (NSInteger)daysAfterDate:(NSDate *)date;
- (NSInteger)daysBeforeDate:(NSDate *)date;
- (NSInteger)yearsAfterDate:(NSDate *)date;
- (NSInteger)yearsBeforeDate:(NSDate *)date;
#pragma mark - Decomposing Dates
- (NSDateComponents *)components:(NSCalendarUnit)unitFlags;
- (NSDateComponents *)componentsSpanningDates:(NSDate *)date;
- (NSDateComponents *)components:(NSCalendarUnit)unitFlags spanningDates:(NSDate *)date;
@property (nonatomic, readonly) NSDateComponents *dateAndTimeComponents;
@property (nonatomic, readonly) NSDateComponents *timeComponents;
@property (nonatomic, readonly) NSInteger nearestHour;
@property (nonatomic, readonly) NSInteger hour;
@property (nonatomic, readonly) NSInteger minute;
@property (nonatomic, readonly) NSInteger second;
@property (nonatomic, readonly) NSInteger nanosecond;
@property (nonatomic, readonly) NSInteger day;
@property (nonatomic, readonly) NSInteger weekday;
@property (nonatomic, readonly) NSInteger nthWeekday;
@property (nonatomic, readonly) NSInteger weekOfYear;
@property (nonatomic, readonly) NSInteger month;
@property (nonatomic, readonly) NSInteger year;
NSDateFormatter
#pragma mark - Factories
+ (instancetype)formatterWithDateFormat:(NSString *)format;
+ (instancetype)formatterWithTimeStyle:(NSDateFormatterStyle)dateStyle;
+ (instancetype)formatterWithDateStyle:(NSDateFormatterStyle)dateStyle;
#pragma mark - RFC's
+ (instancetype)rfc822;
+ (instancetype)rfc3339;
早期工作
我相信该项目原本在2010年某个时间点从 NSDate-Extensions 分支出来,但我不确定其起源。从那时起,我大幅重构、扩展并改进了代码,并相信它们已足够不同,可被视为独立的成果。尽管如此,我还是想感谢 @erica (Erica Sadun) 以及可能启发此项目的任何人。
许可协议
CBHDateKit 在 ISC 许可协议 下提供。