IOS

NSDate的一些简单用法整理

2015-06-21  本文已影响164人  SHChen

A、创建或者初始化的方法

      1、返回现在的时间

       NSDate*now = [NSDate date];

      2、在一定的时间基准上来确定另一个时间

      -  (instancetype) dateByAddingTimeInterval:(NSTimeInterval) ti

      3、在某一个时间的基础上加多少秒之后返回一个新的时间

       - (id)addTimeInterval:(NSTimeInterval)seconds

B、时间之间相互比较的方法

       1、返回两个时间中相对较早的那个时间

        - (NSDate*)earlierDate:(NSDate*)anotherDate;

        2、返回两个时间当中相对较晚的时间

        - (NSDate*)laterDate:(NSDate*)anotherDate;

        3、判断两个时间是否相等

       - (BOOL)isEqualToDate:(NSDate*)otherDate;

       4、将两个时间进行比较

       - (NSComparisonResult)compare:(NSDate*)other;

          得出的结果主要有:

                NSOrderedAscending——后面的时间要大于调用这个方法的时间

                NSOrderedSame——相等

                NSOrderedDescending——后面的时间小于调用这个方法的时间

上一篇下一篇

猜你喜欢

热点阅读