iOSNSDate和NSDateFormatter

iOS开发中常需要对时间进行处理然后得到我们想要的。

NSDate用法

(1)返回系统当前时间

       [NSDate date]
 
(2)返回以指定时间(GMT)为基准,然后过了secs秒的时间
      //增加时间用正数,减少时间用负数(单位是秒)
      [NSDate dateWithTimeInterval:3600 sinceDate:currentDate];

(3)返回以1970/01/01为基准,到某个时间的总秒数。
      [currentDate timeIntervalSince1970]

(4)返回很多年以后的将来某一天。
         [NSDate distantFuture]

(5)返回很多年以前的某一天。
      [NSDate distantPast]

(6)判断两个时间是否相等。
      [future isEqualToDate:past]
          
(7)返回两个时间较早的时间
       [future earlierDate:past]

(8)返回两个时间较晚的时间
       [future laterDate:past];

(9)返回两个时间的时间间隔(单位是秒)
       [afterDate timeIntervalSinceDate:currentDate]


NSDateFormatter用法

(1)初始化(时间格式化类)

      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

(2)设置时间格式
      formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

 

(3)把NSDate转换为NSString

      NSString *currentDateString = [formatter stringFromDate:currentDate];

(4)把NSString转换NSDate

      NSDate *date = [formatter dateFromString:dateString]

附://UTC:Coordinated Universal Time,又称世界统一时间。
       //GMT:格林尼治标准时间。

猜你喜欢

转载自blog.csdn.net/RangingWon/article/details/53915773
今日推荐