zoukankan      html  css  js  c++  java
  • 判断当前时间是否在一个时间段范围内

    + (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
    {
        NSDate *dateFromHour = [self getCustomDateWithHour:fromHour];
        NSDate *dateToHour = [self getCustomDateWithHour:toHour];
        
        NSDate *currentDate = [NSDate date];
        
        if ([currentDate compare:dateFromHour]==NSOrderedDescending && [currentDate compare:dateToHour]==NSOrderedAscending)
        {
            return YES;
        }
        return NO;
    }
    
    
    + (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];
    }
  • 相关阅读:
    JAVA 多线程开篇 -从按顺序打印ABC开始
    学英语
    称砝码
    JAVA BST的实现
    JAVA 引用
    常用查找算法的总结
    bootstrap学习之二-组件
    bootstrap学习之一_bootstrap css
    前端学习——css实用技术
    前端学习——css基础知识,选择器与html模板、值得收藏的html标签
  • 原文地址:https://www.cnblogs.com/joesen/p/4173630.html
Copyright © 2011-2022 走看看