NSDate时间类/NSDateFormatter日期格式类

 

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

 

    // NSDate 时间类 继承自NSObject,表示一个时间点;

    NSDate *date = [NSDate date];

    // 打印结果是:格里尼治时间:零时区(世界标准时间) 年-月-日 时:分:秒 +时区

    NSLog(@"%@", date);

    

    /*******************  NSTimeInterval 时间间隔  ********************/

    

    // [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)]

    // NSTimeInterval(表示时间间隔) 本质就是double数据类型

    // 在零时区的基础上间隔8个小时就是东八区的时间

    // 北京时间

     NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8*60*60];

     NSLog(@"%@", date1);

 

    // 明天的现在时刻

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

     NSLog(@"%@", nextDay);

    

    // 明年的现在时刻

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

     NSLog(@"%@", nextYear);

    

    // 计算给定时间点和当前时间点的时间间隔

    NSTimeInterval interval = nextDay.timeIntervalSinceNow;

    // 得到的是距离标准时间的时间间隔

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

    

    // 计算两个时间点的时间间隔

    NSTimeInterval interval2 = [nextDay timeIntervalSinceDate:date1];

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

 

    /*************************   时间戳  *****************************/

    

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

    NSTimeInterval interval3 = [date timeIntervalSince1970];

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

    

    // 将时间戳转换为时间对象

    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:60];

    NSLog(@"%@", date2);

    

    // 获取北京时间

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

    NSLog(@"%@", date3);

    

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

    // 创建一个固定时间

    NSDate *nowDate = [NSDate date];

    NSDate *pastDate = [NSDate dateWithTimeIntervalSinceNow:-25*3600];

    

    // 计算固定时间和当前时间的时间间隔

    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{

        NSLog(@"%@", pastDate);

    }

 

/******************************   NSDateFormatter 日期格式类   **************************************/

    

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

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

    

    // 设置日期格式中用到的字母的作用: y:年 M:月 d:日 H:时 m:分 s:秒

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

    NSDate *date4 = [NSDate date];

    

    // 将时间对象转为设定格式, 格式化的时候会自动加上距离零时区的时间间隔

    NSString *dateString = [formatter stringFromDate:date4];

    NSLog(@"%@", dateString);

    

    // 反过来将时间字符串转换为NSDate对象

    // 例如: @"2016年05月05号 21时50分20秒"

    

    // 创建时间格式对象

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

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

    // 准备时间字符串

    NSString *dateString1 =  @"2016年05月05号 21时50分20秒";

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

    NSDate *date5 = [formatter1 dateFromString:dateString1];

    // 转过来的时间会回归到零时区时间

    NSLog(@"%@", date5);

    

    // 如果想得到北京时间需要手动加上8小时

    NSDate *date6 = [date5 dateByAddingTimeInterval:8*60*60];

    NSLog(@"%@" ,date6);

    

/******************************   日期之间最常用的比较方法调用   **************************************/

     

     // 与otherDate比较,相同返回YES

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

     

     // 与anotherDate比较,返回较早的那个日期

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

     

     // 与anotherDate比较,返回较晚的那个日期

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

     

     // 该方法用于排序时调用:

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

      当实例保存的日期值与anotherDate相同时返回NSOrderedSame

      当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

      当实例保存的日期值早于anotherDate时返回NSOrderedAscending

     

    return 0;

}

 

转载于:https://www.cnblogs.com/Mr-zyh/p/5463572.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值