zoukankan      html  css  js  c++  java
  • 判断当前时间是否在一天的某个时间段内,可设置时,分,秒

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSDateComponents *fromDateComponents = [[NSDateComponents alloc]init];
        NSDateComponents *toDateComponents = [[NSDateComponents alloc]init];
        [fromDateComponents setHour:6];        // 开始时间-小时
        [toDateComponents setHour:18];         // 结束时间-小时
        [toDateComponents setMinute:58];       // 结束时间-分钟
        
        
        [self isBetweenFromDateComponents:fromDateComponents toComponents:toDateComponents];
        
    }
    
    /**
     *  转化为系统当前地区时间
     *
     *  @param date 系统时间
     *
     *  @return 系统当前地区时间
     */
    - (NSDate *)convertSystemLocationDate:(NSDate *)date
    {
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSTimeInterval interval = [zone secondsFromGMTForDate:date];
        return [date dateByAddingTimeInterval:interval];
    }
    
    /**
     *  获取当前时间是否在设置的某一个时间段内
     *
     *  @param fromComponents 开始时间
     *  @param toComponents   结束时间
     *
     *  @return BOOL
     */
    - (BOOL)isBetweenFromDateComponents:(NSDateComponents*)fromComponents toComponents:(NSDateComponents *)toComponents
    {
        NSDate *fromDate = [self getCustomDateeWithDateComponents:fromComponents];
        NSDate *toDate = [self getCustomDateeWithDateComponents:toComponents];
    
        // 获取当前时间
        NSDate *currDate = [NSDate date];
        if ([[self convertSystemLocationDate:currDate] compare:fromDate]== NSOrderedDescending && [[self convertSystemLocationDate:currDate] compare:toDate] == NSOrderedAscending)
        {
            return YES;
        }
        return NO;
    }
    
    /**
     *  根据设置的时间返回一个NSDate类型的时间
     *
     *  @param dateComponents NSDateComponents
     *
     *  @return NSDate
     */
    - (NSDate *)getCustomDateeWithDateComponents:(NSDateComponents *)dateComponents
    {
        //获取当前时间
        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:[dateComponents hour]];
        [resultComps setMinute:[dateComponents minute]];
        
        NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDate *resultDate = [self convertSystemLocationDate:[resultCalendar dateFromComponents:resultComps]];
        
        return resultDate;
    }
  • 相关阅读:
    scp免密码登陆进行远程文件同步
    MAD(Median absolute deviation, 中位数绝对偏差)
    机器学习之评价准则RoC与PR
    最新HGVS基因突变命名规则速览
    Somatic vs Germline Mutations
    c/c++ 获取文件夹或目录下的文件
    诊断实验评估指标-灵敏度(sensitivity)特异度(specificity)准确度(accuracy)
    互斥量与条件变量(三步走战略)结合使用原理
    linux常用的时间获取函数(time,gettimeofday,clock_gettime,_ftime,localtime,strftime )
    dup和dup2应用实例(dup跟APUE有出入,close+dup=dup2?)
  • 原文地址:https://www.cnblogs.com/joesen/p/4447780.html
Copyright © 2011-2022 走看看