您的位置:首页 > 移动开发 > IOS开发

ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。

2016-11-02 08:59 573 查看
应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内。难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟这俩时间作对比就好了。

下面提供两条思路:

法1.用NSDate生成当前时间,然后转为字符串,从字符串中取出当前的年、月、日,然后再拼上时、分、秒,然后再将拼接后的字符串转为NSDate,最后用当前的时间跟自己生成的俩NSDate的时间点比较。(该方法比较笨,也不难,但看起来有点太菜了,看上去不怎么规范)

法2.用NSDateComponents、NSCalendar确定俩固定的NSDate格式的时间,然后再进行比较(此方法比较装逼,其实跟拼字符串的方法复杂度差不了多少,但看起来比较规范,像是大神写的)。
<span class="javadoc" style="color: rgb(153, 153, 136); font-style: italic;">/**
* <span class="javadoctag">@brief</span> 判断当前时间是否在fromHour和toHour之间。如,fromHour=8,toHour=23时,即为判断当前时间是否在8:00-23:00之间
*/</span>
- (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
{
<span class="indent">  </span>NSDate *date8 = [self getCustomDateWithHour:<span class="number" style="color: rgb(0, 153, 153);">8</span>];
<span class="indent">  </span>NSDate *date23 = [self getCustomDateWithHour:<span class="number" style="color: rgb(0, 153, 153);">23</span>];
<span class="indent">  </span>
<span class="indent">  </span>NSDate *currentDate = [NSDate date];
<span class="indent">  </span>
<span class="indent">  </span><sp
4000
an class="keyword" style="font-weight: bold;">if</span> ([currentDate compare:date8]==NSOrderedDescending && [currentDate compare:date23]==NSOrderedAscending)
<span class="indent">  </span>{
<span class="indent">  </span><span class="indent">  </span>NSLog(@<span class="string" style="color: rgb(221, 17, 68);">"该时间在 %d:00-%d:00 之间!"</span>, fromHour, toHour);
<span class="indent">  </span><span class="indent">  </span><span class="keyword" style="font-weight: bold;">return</span> YES;
<span class="indent">  </span>}
<span class="indent">  </span><span class="keyword" style="font-weight: bold;">return</span> NO;
}

<span class="javadoc" style="color: rgb(153, 153, 136); font-style: italic;">/**
* <span class="javadoctag">@brief</span> 生成当天的某个点(返回的是伦敦时间,可直接与当前时间[NSDate date]比较)
* <span class="javadoctag">@param</span> hour 如hour为“8”,就是上午8:00(本地时间)
*/</span>
- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
<span class="indent">  </span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//获取当前时间</span>
<span class="indent">  </span>NSDate *currentDate = [NSDate date];
<span class="indent">  </span>NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
<span class="indent">  </span>NSDateComponents *currentComps = [[NSDateComponents alloc] init];
<span class="indent">  </span>
<span class="indent">  </span>NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
<span class="indent">  </span>
<span class="indent">  </span>currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
<span class="indent">  </span>
<span class="indent">  </span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//设置当天的某个点</span>
<span class="indent">  </span>NSDateComponents *resultComps = [[NSDateComponents alloc] init];
<span class="indent">  </span>[resultComps setYear:[currentComps year]];
<span class="indent">  </span>[resultComps setMonth:[currentComps month]];
<span class="indent">  </span>[resultComps setDay:[currentComps day]];
<span class="indent">  </span>[resultComps setHour:hour];
<span class="indent">  </span>
<span class="indent">  </span>NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
<span class="indent">  </span><span class="keyword" style="font-weight: bold;">return</span> [resultCalendar dateFromComponents:resultComps];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐