ios正在使用NSDateComponents、NSDate、NSCalendar它的结论是在当前时间是在一段时间在一天。
一般应用程序设置这一组的存在,比如夜间模式,如果你。从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:8];
NSDate *date23 = [self getCustomDateWithHour:23]; 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];
} PS:有一种更好的方式牛逼,随后不久,你说,加群:172158202
ios正在使用NSDateComponents、NSDate、NSCalendar它的结论是在当前时间是在一段时间在一天。的更多相关文章
- ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。
应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟 ...
- NSDate,NSCalendar,NSTimer,NSTimeZone
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...
- supersr--时间显示逻辑-->NSDate+NSCalendar
一种:时间逻辑: - (NSString *)created_at{ // 从后台返回的字符串格式:Mon Aug 03 09:17:31 +0800 2014, //NSDateFormatt ...
- Swift iOS 日期操作:NSDate、NSDateFormatter
1.日期(NSDate) // 1.初始化 // 初始化一个当前时刻对象 var now = NSDate() // 初始化一个明天当前时刻对象 var tomorrow = NSDate(timeI ...
- ios开发日期的NSDate,NSCalendar分类
#import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...
- iOS Foundation框架 -4.NSDate类的简单用法
NSDate为日期时间类对象,简单操作: 注意:直接NSLog输出NSDate对象,默认是以0时区为标准,因此会比北京时间少8小时 1.将Date格式转换为自定义格式的字符串格式 // 自定义Date ...
- iOS-获取的NSDate date时间与实际相差8个小时解决方案
本文转载至 http://blog.sina.com.cn/s/blog_51a995b70101iptl.html NSDate *date = [NSDate date]; NSTimeZone ...
- IOS在后台每隔一段时间执行一下
步骤: 1.在info.plist里加入UIBackgroundModes键,其值为数组,数组之一为voip字符串: <key>UIBackgroundModes</key>& ...
- ios 进入后台 一段时间在进入前台 动画消失
http://www.cnblogs.com/YouXianMing/p/3670846.html
随机推荐
- 高仿快车100--实战RadioGroup和RadioButton应用
1.RadioButtonCheckBox的差别: a.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后.通过点击能够变为未选中 b.一组RadioButton ...
- MFC漆摘要-截图,获得DIB/DDB图形Pixel
1. 当前Screen进行Copy屏幕,获得BITMAP 当前屏幕Copy.须要获取当前屏幕的HDC, 一种是直接从屏幕DC抓原始图. 一种是然后使用兼容MemDC进行抓图,然后能够附加图 ...
- 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch
原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...
- T-SQL基础(1) - T-SQL查询和编程基础
第一范式: 第一范式要求表中的行必须是唯一的,属性应该是原子的(atomic).这个范式对于关系的定义来说是冗余的,换句话说,如果一个表真可以表示一个关系,那么它一定符合第一范式. 行的唯一性是可以通 ...
- EF 主键自增、级联删除
一.主键自增 1.设置数据库中,主键自增 2.设置VS中Model1.edmx
- JDK源代码学习系列07----Stack
JDK源代码学习系列07----Stack 1.Stack源代码很easy ...
- COJ 1102 - You Can Say 11 题解
本题就是给出一个无穷大数,算其能否被11除尽 Description Your job is, given a positive number N, determine if it is a mult ...
- POJ - 3249 Test for Job (DAG+topsort)
Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...
- 程序猿学英语—In July the English learning summary
七月的英语学习更加曲折,七月初.查看更多,周六和周日可以保证每天两小时的英语教室学习.周一到周 五一般是上完晚自习九点多来机房学习英语,学习一个小时十点多再回宿舍. 考完试,回家待了五天,不好意思的说 ...
- spring mvc 错误摘要--。位。
1....identifier of an instance of org.szgzw.ent.profile.baseinfo.enterprise.EnterpriseEntity was alt ...