oc NSDate

 /*
     NSDate 处理时间的类
     默认打印的是 0 时区的时间
     */
    /*
//    实例化一个 NSDate 对象 (使用+date)
    NSDate *date  = [NSDate date];
    NSLog(@"%@",date);//默认是0时区的时间
//    获取东八区的当前时间 相差八小时  8*60*60
    NSDate *nowDate = [NSDate dateWithTimeIntervalSinceNow:8*60*60];
    NSLog(@"%@",nowDate);
//    计算的是在1970-01-01 00-00-00 的基础上加了一个小时
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:1*60*60];
    NSLog(@"%@",date1);
//    计算的是在1970-01-01 00-00-00 的基础上了减一个小时
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:-1*60*60];
    NSLog(@"%@",date2);
//    计算的是在2001-01-01 00-00-00 的基础上加了一个小时
    NSDate *date3 = [NSDate dateWithTimeIntervalSinceReferenceDate:1*60*60];
    NSLog(@"%@",date3);
//    自定义初始时间
    NSDate *date4 = [NSDate dateWithTimeInterval:8*60*60 sinceDate:[NSDate date]];
    NSLog(@"%@",date4);

    NSDate *yesterday = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date4];
    NSLog(@"%@",yesterday);
    
    NSDate *tomorrow = [NSDate dateWithTimeInterval:24*60*60 sinceDate:[NSDate dateWithTimeInterval:8*60*60 sinceDate:[NSDate date]]];
    NSLog(@"%@",tomorrow);
//    计算两个人日期相差的时间
    NSTimeInterval yest = [yesterday timeIntervalSinceDate:tomorrow];
    NSLog(@"%.2lf",yest/3600);
    NSTimeInterval time1 = [tomorrow timeIntervalSinceNow];
    NSLog(@"%.2lf",time1/3600);
    
    
//  ------>>>>>>>>>>>>>>>>>>>  NSDateFormatter 可以设定转化格式,完成 NSString 和 NSDate的转换
    
//    date转换为字符串
//    系统给定格式
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
//    设置日期格式
    [formatter1 setDateStyle:NSDateFormatterFullStyle];
    NSLog(@"%@",[formatter1 stringFromDate:[NSDate date]]);
//    自定义的格式
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
    [formatter2 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
     NSLog(@"%@",[formatter2 stringFromDate:[NSDate date]]);
    
    
//   字符串 转换为 date
    NSDate *d1 = [formatter2 dateFromString:@"2000/02/01 12:12:12"];
    NSLog(@"%@",d1);//打印d1的时候默认打印的是0时区的时间
    */
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值