NSDate+CommonExtension.h 2.93 KB
/*
 Erica Sadun, http://ericasadun.com
 iPhone Developer's Cookbook 3.x and beyond
 BSD License, Use at your own risk
 */

/*
 该NSDate类别的地址如下:
 https://github.com/erica/NSDate-Extensions
 该文件是最新版本。与上一版本主要的改动是两点:
 1.接口中的NSUInteger类型的传入参数改为NSInteger。(防止传入负数的错误)
 2.添加了新的接口:
 - (BOOL)bba_isSameWeekAsDate:(NSDate *)aDate;
 - (BOOL)bba_isThisWeek;
 - (BOOL)bba_isTypicallyWorkday;
 - (BOOL)bba_isTypicallyWeekend;
 
 ---zxh
 */
#import <Foundation/Foundation.h>

#define BBA_MINUTE	60
#define BBA_HOUR	3600
#define BBA_DAY		86400
#define BBA_WEEK	604800
#define BBA_YEAR	31556926

@interface NSDate (CommonExtension)

// Relative dates from the current date
+ (NSDate *)bba_dateTomorrow;
+ (NSDate *)bba_dateYesterday;
+ (NSDate *)bba_dateWithDaysFromNow:(NSInteger)days;
+ (NSDate *)bba_dateWithDaysBeforeNow:(NSInteger)days;
+ (NSDate *)bba_dateWithHoursFromNow:(NSInteger)dHours;
+ (NSDate *)bba_dateWithHoursBeforeNow:(NSInteger)dHours;
+ (NSDate *)bba_dateWithMinutesFromNow:(NSInteger)dMinutes;
+ (NSDate *)bba_dateWithMinutesBeforeNow:(NSInteger)dMinutes;

// Comparing dates
- (BOOL)bba_isEqualToDateIgnoringTime:(NSDate *)aDate;
- (BOOL)bba_isToday;
- (BOOL)bba_isTomorrow;
- (BOOL)bba_isYesterday;
- (BOOL)bba_isSameWeekAsDate:(NSDate *)aDate;
- (BOOL)bba_isThisWeek;
- (BOOL)bba_isNextWeek;
- (BOOL)bba_isLastWeek;
- (BOOL)bba_isSameMonthAsDate:(NSDate *)aDate;
- (BOOL)bba_isThisMonth;
- (BOOL)bba_isSameYearAsDate:(NSDate *)aDate;
- (BOOL)bba_isThisYear;
- (BOOL)bba_isNextYear;
- (BOOL)bba_isLastYear;
- (BOOL)bba_isEarlierThanDate:(NSDate *)aDate;
- (BOOL)bba_isLaterThanDate:(NSDate *)aDate;

// Date roles
- (BOOL)bba_isTypicallyWorkday;
- (BOOL)bba_isTypicallyWeekend;

// Adjusting dates
- (NSDate *)bba_dateByAddingDays:(NSInteger)dDays;
- (NSDate *)bba_dateBySubtractingDays:(NSInteger)dDays;
- (NSDate *)bba_dateByAddingHours:(NSInteger)dHours;
- (NSDate *)bba_dateBySubtractingHours:(NSInteger)dHours;
- (NSDate *)bba_dateByAddingMinutes:(NSInteger)dMinutes;
- (NSDate *)bba_dateBySubtractingMinutes:(NSInteger)dMinutes;
- (NSDate *)bba_dateAtStartOfDay;

// Retrieving intervals
- (NSInteger)bba_minutesAfterDate:(NSDate *)aDate;
- (NSInteger)bba_minutesBeforeDate:(NSDate *)aDate;
- (NSInteger)bba_hoursAfterDate:(NSDate *)aDate;
- (NSInteger)bba_hoursBeforeDate:(NSDate *)aDate;
- (NSInteger)bba_daysAfterDate:(NSDate *)aDate;
- (NSInteger)bba_daysBeforeDate:(NSDate *)aDate;

- (NSTimeInterval)bba_daysDecimalAfterDate:(NSDate *)aDate;
- (NSTimeInterval)bba_hoursDecimalAfterDate:(NSDate *)aDate;

+ (long)bba_timeSecondsSince1970;

// Decomposing dates
- (NSInteger)bba_nearestHour;
- (NSInteger)bba_hour;
- (NSInteger)bba_minute;
- (NSInteger)bba_seconds;
- (NSInteger)bba_day;
- (NSInteger)bba_month;
- (NSInteger)bba_week;
- (NSInteger)bba_weekday;
- (NSInteger)bba_nthWeekday; // e.g. 2nd Tuesday of the month == 2
- (NSInteger)bba_year;


@end