iOS 断当前时间是否在一天的某个时间段内。
应用中设置一般会存在这样的设置,如夜间勿扰模式,从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 断当前时间是否在一天的某个时间段内。的更多相关文章
- ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。
应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟 ...
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- iOS - OC NSDate 时间
前言 NSDate @interface NSDate : NSObject <NSCopying, NSSecureCoding> NSDate 用来表示公历的 GMT 时间(格林威治时 ...
- iOS - Swift NSDate 时间
前言 NSDate public class NSDate : NSObject, NSCopying, NSSecureCoding NSDate 用来表示公历的 GMT 时间(格林威治时间).是独 ...
- iOS 断网处理
iOS 断网处理 (2014-01-13 18:13:21) 转载▼ 标签: it - (BOOL)application:(UIApplication *)application didFini ...
- iOS 获取当前时间格式化字符串
iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...
- iOS判断当前时间是否处于某个时间段内
/** * 判断当前时间是否处于某个时间段内 * * @param startTime 开始时间 * @param expireTime 结束时间 */ - (BOOL)validateWithSta ...
- c# 判断当前时间是否在 工作日时间段内
#region //获取当前周几 private string _strWorkingDayAM = "08:30";//工作时间上午08:00 private string _s ...
- Java判断一个时间是否在另一个时间段内
需求:当时间在凌晨0点至0点5分之间程序不执行. 也就是实现判断当前时间点是否在00:00:00至00:05:00之间 方法: Java代码 : /** * 判断时间是否在时间段内 * * @para ...
随机推荐
- VMware安装Centos7超详细过程(图文)
1.软硬件准备 软件:推荐使用VMwear,我用的是VMwear 12 镜像:CentOS6 ,如果没有镜像可以在阿里云下载 https://mirrors.aliyun.com/centos/ 硬件 ...
- openfireserver和jdk环境删除命令
一.卸载jdk1.8 终端依次运行以下的命令 sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -rf / ...
- OPC UA的监控项、订阅、和通知
MonitoredItem 每个监控项均指明了要监控的项目(item)和用来发送通知的订阅. item可以是一个节点的属性(node attribute). MonitorItem可以监控一个属性,一 ...
- 在Linux下如何查CC攻击?
什么是CC攻击?CC攻击就是利用大量代理服务器对目标计算机发起大量连接,导致目标服务器资源枯竭造成拒绝服务.那么如何判断查询CC攻击呢?本文主要介绍了一些Linux下判断CC攻击的命令. AD:201 ...
- Tokyo Tyrant(TTServer)系列(六)-数据丢失谁的错
,false,1,100);$mem->addServer ("127.9.9.1",1978,false,1,100);$start=microtime(true);for ...
- 点滴积累【other】---存储过程修改表的所有字段(sql)
USE [QG_Mis24] GO /****** Object: StoredProcedure [dbo].[p_set] Script Date: 07/11/2013 17:05:38 *** ...
- @property (nonatomic,retain)中的nonatom和retain的意思
转自:http://yuanshoupeng2005.blog.163.com/blog/static/68235027201235113952886/ http://baike.baidu.com/ ...
- BZOJ 3000(Big Number-Stirling公式求n!近似值)
3000: Big Number Time Limit: 2 Sec Memory Limit: 128 MB Submit: 220 Solved: 62 [Submit][Status] De ...
- C# Lpt 并口热敏小票打印机打印位图
class LptControl { private string LptStr = "lpt1"; public LptControl(string l_LPT_Str) { L ...
- matplotlib之设置极坐标的方向
#!/usr/bin/env python3 #-*- coding:utf-8 -*- ############################ #File Name: polar.py #Auth ...