关于时间的全局变量和宏,下面方法用到。
#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;
获取某个时间的年、月、日、小时、分钟、秒
- (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分类里面实现
一、月末最后一天
/**
月末最后一天
@param date 当前时间对象
@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 当前时间对象
@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;
}
三、本周周一
/**
本周周一 一周时间起点按周一算起(周一、周二、周三、周四、周五、周六、周日)
@param date 当前时间对象
@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;
}
四、本周周日
/**
本周周日 一周时间起点按周日算起(周日、周一、周二、周三、周四、周五、周六)
@param date 当前时间对象
@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'月'前或者后时间
//某个时间前或者后N个月
+ (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'天'前或者后时间
//N天后时间
- (NSDate *) dateByAddingDays: (NSInteger) dDays
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_DAY * dDays;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
//N'天'前时间
- (NSDate *) dateBySubtractingDays: (NSInteger) dDays
{
return [self dateByAddingDays: (dDays * -1)];
}
七、N'分钟'前或者后时间
//N分钟后时间
- (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
//N分钟前时间
- (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes
{
return [self dateByAddingMinutes: (dMinutes * -1)];
}
八、N'小时'前或者后时间
//N小时后时间
- (NSDate *) dateByAddingHours: (NSInteger) dHours
{
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_HOUR * dHours;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
//N小时前时间
- (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));
}
十二、关于周 是否属于同一周、 是否本周、是否上一周、是否为下周
//是否属于同一周
- (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];
}
十三、关于月的 是否同一个月、是否本月
//是否属于同一个月
- (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]];
}
十四、关于年的 是否属于同一年、是否为今年、是否上一年、是否下一年
//是否属于同一年
- (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));
}
十五、时间大小比较
//大于当前时间
- (BOOL) isEarlierThanDate: (NSDate *) date
{
return ([self compare: date] == NSOrderedAscending);
}
//小于当前时间
- (BOOL) isLaterThanDate: (NSDate *) date
{
return ([self compare: date] == NSOrderedDescending);
}
二、设置时间戳 实现可以在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;
}