關(guān)于時(shí)間的全局變量和宏,下面方法用到砌创。
#define DATE_COMPONENTS (NSCalendarUnitYear \
| NSCalendarUnitMonth \
| NSCalendarUnitDay \
| NSCalendarUnitWeekOfYear \
| NSCalendarUnitHour \
| NSCalendarUnitMinute \
| NSCalendarUnitSecond \
| NSCalendarUnitWeekday \
| NSCalendarUnitWeekdayOrdinal)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]
static CGFloat const D_MINUTE = 60;
static CGFloat const D_HOUR = 3600;
static CGFloat const D_DAY = 86400;
static CGFloat const D_WEEK = 604800;
static CGFloat const D_YEAR = 31556926;
獲取某個(gè)時(shí)間的年虏缸、月、日嫩实、小時(shí)刽辙、分鐘、秒
- (NSInteger)year
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
return [dateComponents year];
}
- (NSInteger)month
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
return [dateComponents month];
}
- (NSInteger)day
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
return [dateComponents day];
}
- (NSInteger)hour
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
return [dateComponents hour];
}
- (NSInteger)minute
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
return [dateComponents minute];
}
- (NSInteger)second
{
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: DATE_COMPONENTS fromDate:self];
return [dateComponents second];
}
/**
* N'天'后日期
*/
NSDate *nextDate = [NSDate dateWithTimeInterval:D_DAY * days sinceDate:self.currentDate];
/**
* N'天'前日期
*/
NSDate *nextDate = [NSDate dateWithTimeInterval: - D_DAY * days sinceDate:self.currentDate];
全部在NSDate分類里面實(shí)現(xiàn)
一甲献、月末最后一天
/**
月末最后一天
@param date 當(dāng)前時(shí)間對(duì)象
@return 月末日期
*/
+ (NSDate *)lastDayOfMonthWithDate:(NSDate *)date
{
double interval = 0;
NSDate *firstDate = nil;
NSDate *lastDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
BOOL OK = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:date];
if (OK) {
lastDate = [firstDate dateByAddingTimeInterval:interval - 1];
}
return lastDate;
}
二宰缤、月初第一天
/**
月初第一天
@param date 當(dāng)前時(shí)間對(duì)象
@return 月初日期
*/
+ (NSDate *)firstDayOfMonthWithDate:(NSDate *)date
{
double interval = 0;
NSDate *firstDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:date];
return firstDate;
}
三、本周周一
/**
本周周一 一周時(shí)間起點(diǎn)按周一算起(周一晃洒、周二慨灭、周三、周四球及、周五氧骤、周六、周日)
@param date 當(dāng)前時(shí)間對(duì)象
@return 本周周一
*/
+ (NSDate *)mondayOfWeekWithDate:(NSDate *)date {
NSTimeInterval interval;
NSDate *mondayDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2]; //Override locale to make week start on Monday
[calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:& mondayDate interval:&interval forDate:date];
return mondayDate;
}
四吃引、本周周日
/**
本周周日 一周時(shí)間起點(diǎn)按周日算起(周日筹陵、周一、周二镊尺、周三朦佩、周四、周五鹅心、周六)
@param date 當(dāng)前時(shí)間對(duì)象
@return 本周周日
*/
+ (NSDate *)sundayOfWeekWithDate:(NSDate *)date {
NSTimeInterval interval;
NSDate *sundayDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:& sundayDate interval:&interval forDate:date];
return sundayDate;
}
五吕粗、N'月'前或者后時(shí)間
//某個(gè)時(shí)間前或者后N個(gè)月
+ (NSDate *)getForwardOrLaterDateFromDate:(NSDate *)currentDate month:(NSInteger)month {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:month];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *tempDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
return tempDate;
}
六纺荧、N'天'前或者后時(shí)間
//N天后時(shí)間
- (NSDate *) dateByAddingDays: (NSInteger) dDays
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_DAY * dDays;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
//N'天'前時(shí)間
- (NSDate *) dateBySubtractingDays: (NSInteger) dDays
{
return [self dateByAddingDays: (dDays * -1)];
}
七旭愧、N'分鐘'前或者后時(shí)間
//N分鐘后時(shí)間
- (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
//N分鐘前時(shí)間
- (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes
{
return [self dateByAddingMinutes: (dMinutes * -1)];
}
八、N'小時(shí)'前或者后時(shí)間
//N小時(shí)后時(shí)間
- (NSDate *) dateByAddingHours: (NSInteger) dHours
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_HOUR * dHours;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
//N小時(shí)前時(shí)間
- (NSDate *) dateBySubtractingHours: (NSInteger) dHours
{
return [self dateByAddingHours: (dHours * -1)];
}
九宙暇、昨天
/**
* 昨天
*
* @return 昨天日期
*/
+ (NSDate *) dateYesterday
{
return [[NSDate date] dateBySubtractingDays:1];
}
十输枯、明天
/**
* 明天
*
* @return 明天日期
*/
+ (NSDate *) dateTomorrow
{
rreturn [[NSDate date] dateByAddingDays:1];
}
十一、是否為今天占贫、是否為明天桃熄、是否為昨天
//是否為今天
- (BOOL) isToday
{
return [self isEqualToDateIgnoringTime:[NSDate date]];
}
//是否為明天
- (BOOL) isTomorrow
{
return [self isEqualToDateIgnoringTime:[NSDate dateTomorrow]];
}
//是否為昨天
- (BOOL) isYesterday
{
return [self isEqualToDateIgnoringTime:[NSDate dateYesterday]];
}
- (BOOL) isEqualToDateIgnoringTime: (NSDate *) aDate
{
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:DATE_COMPONENTS fromDate:self];
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:DATE_COMPONENTS fromDate:aDate];
return ((components1.year == components2.year) &&
(components1.month == components2.month) &&
(components1.day == components2.day));
}
十二、關(guān)于周 是否屬于同一周型奥、 是否本周瞳收、是否上一周碉京、是否為下周
//是否屬于同一周
- (BOOL) isSameWeekWithDate: (NSDate *) date
{
NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate: date];
if (components1.weekOfYear != components2.weekOfYear) return NO;
return (fabs([self timeIntervalSinceDate:aDate]) < D_WEEK);
}
//是否為本周
- (BOOL) isThisWeek
{
return [self isSameWeekWithDate:[NSDate date]];
}
//是否為下一周
- (BOOL) isNextWeek
{
NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_WEEK;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return [self isSameWeekWithDate:newDate];
}
//是否為上一周
- (BOOL) isLastWeek
{
NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] - D_WEEK;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return [self isSameWeekWithDate:newDate];
}
十三、關(guān)于月的 是否同一個(gè)月螟深、是否本月
//是否屬于同一個(gè)月
- (BOOL) isSameMonthWithDate: (NSDate *) date
{
NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate: date];
return ((components1.month == components2.month) &&
(components1.year == components2.year));
}
//是否為本月
- (BOOL) isThisMonth
{
return [self isSameMonthWithDate:[NSDate date]];
}
十四谐宙、關(guān)于年的 是否屬于同一年、是否為今年界弧、是否上一年凡蜻、是否下一年
//是否屬于同一年
- (BOOL) isSameYearWithDate: (NSDate *) date
{
NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate: date];
return (components1.year == components2.year);
}
//是否為今年
- (BOOL) isThisYear
{
return [self isSameYearWithDate:[NSDate date]];
}
//是否為下一年
- (BOOL) isNextYear
{
NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:[NSDate date]];
return (components1.year == (components2.year + 1));
}
//是否為前年
- (BOOL) isLastYear
{
NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:[NSDate date]];
return (components1.year == (components2.year - 1));
}
十五、時(shí)間大小比較
//大于當(dāng)前時(shí)間
- (BOOL) isEarlierThanDate: (NSDate *) date
{
return ([self compare: date] == NSOrderedAscending);
}
//小于當(dāng)前時(shí)間
- (BOOL) isLaterThanDate: (NSDate *) date
{
return ([self compare: date] == NSOrderedDescending);
}
二垢箕、設(shè)置時(shí)間戳 實(shí)現(xiàn)可以在NSString 分類中 或者工具類中
+ (NSString *)getDateWithTargetDate:(NSDate *)targetDate formatter:(NSString *)formatter {
if (!targetDate) {
return @"";
}
if (!formatter) {
formatter = @"yyyy-MM-dd HH:mm";
}
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
dateformatter.dateFormat = formatter;
NSString * tempString=[dateformatter stringFromDate:targetDate];
return tempString;
}