Objective-C学习篇10—NSDate与NSDateFormatter

Objective-C学习篇10—NSDate与NSDateFormatter

 

 NSDate

 NSDate 时间类,继承自NSObject,其对象表示一个时间点

    NSDate *date = [NSDate date];

    NSLog(@"date = %@", date);

  2015-12-04 19:08:00.624 OCNSDate[2955:309612] date = 2015-12-04 11:08:00 +0000

  打印显示的是格里尼治时间 年-月-日 时:分:秒 + 时区

    

    

  1. 得到一个距离当前时间间隔时间点的创建方法  dateWithTimeIntervalSinceNow:(NSTimeInterval)

  (NSTimeInterval) 的本质是double数据类型

    NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8*60*60];  // 用格里尼治时间加8个小时的时差,得到北京时间

    NSLog(@"date1 = %@", date1);

  2015-12-04 19:08:00.625 OCNSDate[2955:309612] date1 = 2015-12-04 19:08:00 +0000

 

复制代码
    练习1:得到明天当前的时间点

    NSDate *nextDay = [NSDate dateWithTimeIntervalSinceNow:24*60*60 + 8*60*60];

    练习2:得到明年的时间点

    NSDate *nextYear = [NSDate dateWithTimeIntervalSinceNow:366*24*60*60 + 8*60*60];

    NSLog(@"%@", nextYear);
复制代码

  2. 计算给定时间和当前时间点的时间间隔     .timeIntervalSinceNow

    NSTimeInterval interval = nextDay.timeIntervalSinceNow;

    NSLog(@"%.2f", interval);

    

 3. 计算两个时间点的时间间隔  timeIntervalSinceDate:

    NSTimeInterval interval2 = [date timeIntervalSinceDate:nextYear];

    NSLog(@"%.2f", interval2);

    

  时间戳的概念: 一个时间点距离 1970.1.1 的时间间隔,这个时间是以秒为单位,就叫做时间戳

  时间戳的求法: timeIntervalSince1970

    NSTimeInterval interval3 = [date timeIntervalSince1970];

    NSLog(@"%.2f", interval3);

    

  将时间戳转化为时间对象  dateWithTimeIntervalSince1970:

    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:3600];

    NSLog(@"%@", date2);

    

  获取北京时间   dateByAddingTimeInterval:

    NSDate *date3 = [date dateByAddingTimeInterval:8*60*60];

    NSLog(@"%@", date3);

    

  练习3: 一个当前时间和一个固定时间的差值,如果差值在60秒之内,则输出"刚刚",如果时间差值在60~3600秒,则输出在"xx分钟之前", 如果在3600~24*3600之内,则输出在"xx小时之前",如果在24*3600秒之外输出固定的时间

复制代码
  固定时间

    NSDate *pastDate = [NSDate dateWithTimeIntervalSinceNow:-370];

    NSLog(@"%@", pastDate);

  当前时间 NSDate *nowDate = [NSDate date];    固定时间与当前时间的差值 NSTimeInterval interval4 = [nowDate timeIntervalSinceDate:pastDate]; NSLog(@"时间差为 %.2f 秒", interval4);
if (interval4 <= 60) { NSLog(@"刚刚"); }else if(interval4 <= 3600){ NSLog(@"%.f分钟之前", interval4 / 60);
}else if(interval4 <= 24*3600){ NSLog(@"%.f小时之前", interval4 / 3600); }else if(interval4 > 24*3600){ NSLog(@"%@", pastDate); }
复制代码

NSDateFormatter

  NSDateFormatter 日期格式类,继承自NSFormatter,主要作用是将NSDate对象转换为某种格式,然后以字符串的形式输出

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

  1. 设置日期格式中用到的字母: y 代表年, M 代表 月, d 代表天 H 代表时 m 代表分, s 代表秒

    [formartter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];

    NSDate *date4 = [NSDate dateWithTimeIntervalSinceNow:0];

  2. 将时间对象转为设定的格式     stringFromDate:

  格式化的时候系统会自动加上距离零时区的时间间隔

    NSString *dateString = [formartter stringFromDate:date4];

    NSLog(@"%@", dateString);

  练习: 将date4以@"2015年份09月份24号 11点43分20秒"的形式输出

  创建一个时间格式类对象  init方法创建

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

  指定输出格式   setDateFormat:@"具体时间格式"

    [myFormatter setDateFormat:@"yyyy年份MM月份dd号 HH点mm分ss秒"];

  用一个字符串接收转化后的时间   stringFromDate:

    NSString *dateString1 = [myFormatter stringFromDate:date4];

    NSLog(@"%@", dateString1);

    

  将时间字符串转换为NSDate对象

复制代码
  例如:  @"2015年11月24号 11时10分10秒"

    NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
    [formatter3 setDateFormat:@"yyyy年MM月dd号 HH时mm分ss秒"];

  准备时间字符串
    NSString *dateString3 = @"2015年11月24号 11时10分10秒";


  使用时间格式对象借助时间字符串格式化时间对象

    NSDate *date5 = [formatter3 dateFromString:dateString3];
  转过来的时间会被回归到零时区的时间 NSLog(@"%@", date5);   如果想得到北京时间需要手动加上8小时 NSDate *date6 = [date5 dateByAddingTimeInterval:8*60*60]; NSLog(@"%@", date6);
 
复制代码

自己选的路,跪着也要走下去......
原文地址:https://www.cnblogs.com/zmc815/p/5325848.html