根据某个日期和所选择的是周几(或者几号),返回大于这个日期,最近的一个时间。
比如:
1.某周的某一星期
currentDate:2016-4-22 10:00 (星期五)
tweekDay:0(星期日)
返回的结果:2016-4-24 10:00 (星期日)
//每周的某一天 星期日为0,tWeekDay取值范围0-6(星期日-星期六)
-(NSDate*)dealWithDate:(NSInteger)tWeekDay currentDate:(NSDate *)aCurrentDate{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_8_0
NSInteger unitFlagsYearMonthDay = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSInteger unitFlagsHourMinuteSecond =NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond ;
NSInteger unitFlagsWeekday = NSCalendarUnitWeekday;
NSInteger unitFlagsWeekOfMonth = NSCalendarUnitWeekOfMonth;
#else
NSInteger unitFlagsYearMonthDay = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSInteger unitFlagsHourMinuteSecond =NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit ;
NSInteger unitFlagsWeekday = NSWeekdayCalendarUnit;
NSInteger unitFlagsWeekOfMonth = NSWeekCalendarUnit;
#endif
NSDate *dateOut = nil;
NSTimeInterval count = 0;
NSCalendar *calendar = [NSCalendar currentCalendar];
//获取当前周的开始日期
BOOL b = [calendar rangeOfUnit:unitFlagsWeekOfMonth startDate:&dateOut interval:&count forDate:aCurrentDate];
if(b)
{
//得到当前周的开始日期的年月日
NSDateComponents *comptGetDayMonthYear = [calendar components:unitFlagsYearMonthDay fromDate:dateOut];
//得到currentDate的星期
NSDateComponents *comptGetWeekDay = [calendar components:unitFlagsWeekday fromDate:aCurrentDate];
//得到currentDate的时间
NSDateComponents *comptGetHourMinuteSecond = [calendar components:unitFlagsHourMinuteSecond fromDate:aCurrentDate];
//设置当前周的开始日期的时间为currentDate的时间
[comptGetDayMonthYear setHour:comptGetHourMinuteSecond.hour];
[comptGetDayMonthYear setMinute:comptGetHourMinuteSecond.minute];
[comptGetDayMonthYear setSecond:comptGetHourMinuteSecond.second];
NSDate *tWeekDayDate = [calendar dateFromComponents:comptGetDayMonthYear];
NSTimeInterval tTimeInterval = 0;
//小于今天的星期时
if (tWeekDay < (comptGetWeekDay.weekday - 1)) {
tTimeInterval = (tWeekDay+7) *60*60*24;
}else{
tTimeInterval = (tWeekDay) *60*60*24;
}
NSDate *tFinalDate = [tWeekDayDate dateByAddingTimeInterval:tTimeInterval];
return tFinalDate;
}
return aCurrentDate;
}
2.某月的某一天
currentDate:2016-4-22 10:00
aMonthDay:1(某月的1号)
返回的结果:2016-5-1 10:00
//某月的某一天,aMonthDay取值范围0-31(1号-31号)
-(NSDate*)dealWithMonthDate:(NSInteger)aMonthDay currentDate:(NSDate *)aCurrentDate{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_8_0
NSInteger unitFlagsYearMonthDay = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSInteger unitFlagsHourMinuteSecond =NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond ;
NSInteger unitFlagsDay = NSCalendarUnitDay;
NSInteger unitFlagsMonth = NSCalendarUnitMonth;
#else
NSInteger unitFlagsYearMonthDay = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSInteger unitFlagsHourMinuteSecond =NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit ;
NSInteger unitFlagsDay = NSDayCalendarUnit;
NSInteger unitFlagsMonth = NSMonthCalendarUnit;
#endif
NSDate *dateOut = nil;
NSTimeInterval count = 0;
NSCalendar *calendar = [NSCalendar currentCalendar];
//获取当前月的开始日期
BOOL b = [calendar rangeOfUnit:unitFlagsMonth startDate:&dateOut interval:&count forDate:aCurrentDate];
if(b)
{
//
NSDateComponents *comptGetDayMonthYear = [calendar components:unitFlagsYearMonthDay fromDate:dateOut];
NSDateComponents *comptGetDay = [calendar components:unitFlagsDay fromDate:aCurrentDate];
NSDateComponents *comptGetHourMinuteSecond = [calendar components:unitFlagsHourMinuteSecond fromDate:aCurrentDate ];
[comptGetDayMonthYear setHour:comptGetHourMinuteSecond.hour];
[comptGetDayMonthYear setMinute:comptGetHourMinuteSecond.minute];
[comptGetDayMonthYear setSecond:comptGetHourMinuteSecond.second];
NSDate *tWeekDayDate = [calendar dateFromComponents:comptGetDayMonthYear];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:tWeekDayDate];
NSInteger tMonthDayCount = range.length;
NSInteger tCount = 1;
//如果当前月的最大天数大于所选的日期
while ((aMonthDay+1) > tMonthDayCount) {
[comptGetDayMonthYear setMonth:(comptGetDayMonthYear.month +tCount)];
tWeekDayDate = [calendar dateFromComponents:comptGetDayMonthYear];
range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:tWeekDayDate];
tMonthDayCount = range.length;
tCount++;
}
//所选日子小于今天的日期
if ((aMonthDay+1) < comptGetDay.day) {
NSTimeInterval tTimeInterval = 60*60*24*(tMonthDayCount + aMonthDay);
NSDate *tFinalDate = [tWeekDayDate dateByAddingTimeInterval:tTimeInterval];
return tFinalDate;
}else{
NSTimeInterval tTimeInterval = (aMonthDay)*60*60*24;
NSDate *tFinalDate = [tWeekDayDate dateByAddingTimeInterval:tTimeInterval];
return tFinalDate;
}
}
return aCurrentDate;
}