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

下面提供两条思路:

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

法2.用NSDateComponents、NSCalendar确定俩固定的NSDate格式的时间,然后再进行比较(此方法比较装逼,其实跟拼字符串的方法复杂度差不了多少,但看起来比较规范,像是大神写的)。

/**
* @brief 判断当前时间是否在fromHour和toHour之间。如,fromHour=8,toHour=23时,即为判断当前时间是否在8:00-23:00之间
*/
- (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
{
NSDate *date8 = [self getCustomDateWithHour:];
NSDate *date23 = [self getCustomDateWithHour:]; NSDate *currentDate = [NSDate date]; if ([currentDate compare:date8]==NSOrderedDescending && [currentDate compare:date23]==NSOrderedAscending)
{
NSLog(@"该时间在 %d:00-%d:00 之间!", fromHour, toHour);
return YES;
}
return NO;
} /**
* @brief 生成当天的某个点(返回的是伦敦时间,可直接与当前时间[NSDate date]比较)
* @param hour 如hour为“8”,就是上午8:00(本地时间)
*/
- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
//获取当前时间
NSDate *currentDate = [NSDate date];
NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentComps = [[NSDateComponents alloc] init]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; currentComps = [currentCalendar components:unitFlags fromDate:currentDate]; //设置当天的某个点
NSDateComponents *resultComps = [[NSDateComponents alloc] init];
[resultComps setYear:[currentComps year]];
[resultComps setMonth:[currentComps month]];
[resultComps setDay:[currentComps day]];
[resultComps setHour:hour]; NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
return [resultCalendar dateFromComponents:resultComps];
}

2.

http://stackoverflow.com/questions/12238395/check-if-the-current-time-is-in-between-time-a-and-b

3.

http://stackoverflow.com/questions/4084341/how-to-calculate-time-in-hours-between-two-dates-in-ios

3.

http://stackoverflow.com/questions/13965921/ios-check-whether-current-time-is-between-two-times-or-not

iOS 断当前时间是否在一天的某个时间段内。的更多相关文章

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

    应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟 ...

  2. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  3. iOS - OC NSDate 时间

    前言 NSDate @interface NSDate : NSObject <NSCopying, NSSecureCoding> NSDate 用来表示公历的 GMT 时间(格林威治时 ...

  4. iOS - Swift NSDate 时间

    前言 NSDate public class NSDate : NSObject, NSCopying, NSSecureCoding NSDate 用来表示公历的 GMT 时间(格林威治时间).是独 ...

  5. iOS 断网处理

    iOS 断网处理 (2014-01-13 18:13:21) 转载▼ 标签: it   - (BOOL)application:(UIApplication *)application didFini ...

  6. iOS 获取当前时间格式化字符串

    iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...

  7. iOS判断当前时间是否处于某个时间段内

    /** * 判断当前时间是否处于某个时间段内 * * @param startTime 开始时间 * @param expireTime 结束时间 */ - (BOOL)validateWithSta ...

  8. c# 判断当前时间是否在 工作日时间段内

    #region //获取当前周几 private string _strWorkingDayAM = "08:30";//工作时间上午08:00 private string _s ...

  9. Java判断一个时间是否在另一个时间段内

    需求:当时间在凌晨0点至0点5分之间程序不执行. 也就是实现判断当前时间点是否在00:00:00至00:05:00之间 方法: Java代码 : /** * 判断时间是否在时间段内 * * @para ...

随机推荐

  1. resin设置jvm参数

    http://www.quiee.com.cn/archives/592/ resin resin3.0 及前期版本内存设置, 如下: 启动时设置虚拟内存: unix> bin/httpd.sh ...

  2. stm32中的数据类型定义

    STM32F10X.H #include "core_cm3.h" #include "system_stm32f10x.h" #include <std ...

  3. atitit。html css框架Bootstrap  Foundation的比较与不同 attilax大总结

    atitit.html css框架Bootstrap  Foundation的比较与不同 attilax大总结 1. Bootstrap  Foundation的比较与不同1 2. Bootstrap ...

  4. JavaScript No Overloading 函数无重载之说

    在ECMAScript语言中,函数名字仅仅只是是一个指针(能够觉得是引用),以下的代码: "use strict"; function sum(a,b){ return a+b; ...

  5. 解决国内gem不能用的问题

    转自:http://www.haorooms.com/post/gem_not_use 最近在安装SASS的时候,用到gem命令,但是运行出行如下错误! C:\Users\len>gem ins ...

  6. Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法

    /** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...

  7. 【JMeter性能测试】之学习资料总结(持续更新)

    本人测试小白,总结一下JMeter性能测试相关文档进行转载学习,下面会贴出原文作者以示感谢: JMeter性能测试学习地址:http://www.ltesting.net/ceshi/open/kyx ...

  8. golang json数组拼接

    2016年06月16日 15:38:25 阅读数:2575 标签: golangjson数组 更多 个人分类: golang   func main() { a := []byte(`{"P ...

  9. 14 javaBean 组件

    bean类不应该有公开的实例变量. 持续性的值应该通过 getXxx 和 setXxx 方法访问. <jsp: useBean id=”beanName” class=”package.Clas ...

  10. Oracle记录学习

    --基本函数--select name,count(id) from work_test group by name having count(id)>1--select upper(name) ...