美文网首页
IOS中关于两个时间点的比对问题(NSDate)

IOS中关于两个时间点的比对问题(NSDate)

作者: silencerZiBo | 来源:发表于2017-03-03 17:22 被阅读1739次

    IOS开发中获取当前时间的方法是通过NSDate获取的。而NSDate是时间在计算机系统内的绝对概念,具有唯一性,不可变性。但是实际开发中,常常需要将时间进行某种历法下的比对(一般常用的是农历、公历)。那么这时的两个NSDate就需要利用特定的历法转化时间戳来比较两个时间的差值。
    这里需要了解相关的3个对象

    NSDate            //获取时间
    NSCalendar        //历法对象
    NSDateComponents  //表示的时间默认以公历(即阳历)为参考
    

    1.NSDate是独立于历法之外的,它只是时间相对于某个时间点的时间差;这点切记
    2.NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。

    //获取历法对象的初始化方法
    //方法1
    NSCalendar *greCalender = [NSCalendar currentCalendar]; /*初始化一个公历历法对象*/
    //方法2
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  /*初始化一个公历历法对象*/
    /*
    NSGregorianCalendar  只是公历,还支持多种历法 ,比如NSChineseCalendar
    */
    
    
    1. NSDateComponents是按照NSCalendar的历法,将NSDate转换成我们能够理解的时间格式。支持年、月、日、时、分、秒。
      dateComponents有非常丰富的关于时间转换、比较等的功能。主要介绍时间的转化和两个Date的对比。

    A 时间戳转化年月日

    void timeTestFunction(){
            //初始化公历历法
            NSCalendar *greCakendar = [NSCalendar currentCalendar];
            //初始化NSDateComponents对象
            /*
             components即对时间戳可转化的对象格式,比如:
             NSCalendarUnitYear  代表翻译成年、NSCalendarUnitMonth  代表翻译成月等等
             注意:需要打印出什么时间单位,这里必须设置。比如需要该时间戳的天,必须有NSCalendarUnitDay,否则是不会转化的
             特殊:NSCalendarUnitWeekOfYear    一年中的第几周
                  NSCalendarUnitWeekOfMonth     一年中的第几个月
             */
            NSDateComponents * dateComponents = [greCakendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth
                                                               fromDate:[NSDate date]];
            
            NSLog(@"年份:%li",(long)dateComponents.year);
            NSLog(@"月份:%li",(long)dateComponents.month);
            NSLog(@"天数:%li",(long)dateComponents.day);
            NSLog(@"小时:%li",(long)dateComponents.hour);
            NSLog(@"本年度第几周:%li",(long)dateComponents.weekOfYear);
            NSLog(@"本月第几周:%li",(long)dateComponents.weekOfMonth);
    }
    

    B 两个NSDate对象对比时间差

    void calculateTheDifferenceBetweenOldTimeAndNewTime()
    {
        //初始化公历历法
        NSCalendar *greCalender = [NSCalendar currentCalendar];
        
        /*
         NSDateComponents的另一个用法:设置一个特定的时间单位,并将其转化为NSdate
         */
        NSDateComponents * dateComponents = [[NSDateComponents alloc] init];
        [dateComponents setDay:1];
        [dateComponents setMonth:1];
        [dateComponents setYear:1990];
        
        //根据设置的dateComponents获取历法中对象的时间点
        //这里的是分秒会使用NSDateComponents中规定的默认数值,一般为0和1
        NSDate * fromDateComponentsDate = [greCalender dateFromComponents:dateComponents];
        
        //在设置的历法中,依照比对的条件返回两个date之间的时间间隔
        NSDateComponents * differenceComponents = [greCalender components:NSCalendarUnitYear
                                                                 fromDate:fromDateComponentsDate
                                                                   toDate:[NSDate date] options:0];
        NSLog(@"90年距今%li年",differenceComponents.year);
    }
    

    C 获取一个某历法下XX时间后的时间点

    NSCalendar *greCalender = [NSCalendar currentCalendar];
    //获取一个dateComponents并设置时间段
        NSDateComponents * dateComponentsAsTimeSet = [[NSDateComponents alloc] init];
        [dateComponentsAsTimeSet setDay:6];
        //获取当前历法下的6天后的时间点
    /*
    这里还可以自己设年、月、日、时、分、秒,根据个人需求
    */
        NSDate * setTimeDate = [greCalender dateByAddingComponents:dateComponentsAsTimeSet
                                                            toDate:[NSDate date] options:0];
        NSLog(@"%@",setTimeDate);
    

    相关文章

      网友评论

          本文标题:IOS中关于两个时间点的比对问题(NSDate)

          本文链接:https://www.haomeiwen.com/subject/zcdrgttx.html