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];
    }
  • 相关阅读:
    CF #536div2E(dp)
    BZOJ2440(容斥+莫比乌斯函数)
    莫比乌斯反演题目结(下)
    struts 文件上传示例
    struts 文件上传示例
    struts2请求过程源码分析
    struts2请求过程源码分析
    如何快速成为数据分析师?
    如何快速成为数据分析师?
    多对多 hibernate映射
  • 原文地址:https://www.cnblogs.com/joesen/p/4173630.html
Copyright © 2011-2022 走看看