zoukankan      html  css  js  c++  java
  • iOS中的NSdate

      @autoreleasepool {
           
            /*
            //创建NSDate 对象,创建的NSDate 对象是零时区的时间
            
            NSDate *currentDate = [NSDate date];
            NSLog(@"%@", currentDate);
           
            //dateWithTimeInterval是以秒计算的
           
            NSDate *BJDate1 = [NSDate dateWithTimeInterval:8 sinceDate:currentDate];
            NSLog(@"%@",BJDate1);
            //通过每一时间,获取另一时间
            //获取给定时间开始相隔多少秒之后的时间,该方法看似获取到的是北京时间,其实还是零时区时间,只不过是现在的零时区再过8小时之后的时间
          NSDate *BJDate = [NSDate dateWithTimeInterval:8*3600 sinceDate:currentDate];
            NSLog(@"%@",BJDate);
            
            //获取明日时间?
           NSDate *tomorrowDate = [NSDate dateWithTimeInterval:24*3600 sinceDate:BJDate];
            NSLog(@"%@",tomorrowDate);
          //获取昨天的时间
            NSDate *yesterdayDate = [NSDate dateWithTimeInterval:-24*3600 sinceDate:BJDate];
            NSLog(@"%@",yesterdayDate);
            
            //获取时间间隔
            
            
            //获取从1970年到现在的时间间隔
           NSTimeInterval timeInterval1970 = [BJDate timeIntervalSince1970];
            NSLog(@"%f", timeInterval1970);
            
            //获取从1970年开始,相差timeInterVal1970 这么长的时间之后的时间
            
            NSDate *timeSince1970 = [NSDate dateWithTimeIntervalSince1970:timeInterval1970];
            NSLog(@"%@",timeSince1970);
            
            //获取从2001年1月1日开始到到现在时间间隔
           NSTimeInterval temeSince2001 = [BJDate timeIntervalSinceReferenceDate];
            
            NSLog(@"%f",temeSince2001);
    
            
            //以当前时间为基准,计算某一个时间与当前时间间隔
            NSTimeInterval timeInterval1 = [tomorrowDate timeIntervalSinceNow];
            NSLog(@"%f",timeInterval1 / 3600);
            
            //以给定时间为基准,获取两个时间间隔
            NSTimeInterval timeIntreval2 = [yesterdayDate timeIntervalSinceDate:tomorrowDate];
            NSLog(@"%f",timeIntreval2 / 3600);
            
            
            
            //比较两个日期是否相等
            BOOL isEqual = [tomorrowDate isEqualToDate:yesterdayDate];
            NSLog(@"%d", isEqual);
            //获取两个时间中比较早的时间
            NSDate *earlyDate = [tomorrowDate earlierDate:yesterdayDate];
            NSLog(@"%@", earlyDate);
            
            //获取两个时间中比较晚的时间
            NSDate *laterDate = [tomorrowDate laterDate:yesterdayDate];
            NSLog(@"%@", laterDate);
            
            //两个日期对象比较
            NSComparisonResult result = [tomorrowDate compare: yesterdayDate];
            NSLog(@"%lu", result);
    
            
            */
            
            //日期转换处理
            
            //NSDate 和 NSString 之间的转换
    //        //1.NSDate 转化成NSString
    //        //1.创建NSDate 对象
             NSDate *currentDate = [NSDate date];
    //        //2.创建日期转换工具
             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //        //3.设定日期转换格式
    //        [formatter setDateFormat:@"yyyy年MM月dd日 E aHH:mm:ss"];
    //        //4.通过日期转换工具,将NSDate的对象currentDate转换成字符串
    //        NSString *timeStr =[formatter stringFromDate:currentDate];
    //        NSLog(@"%@", timeStr);
    //        
            /**
             *  日期格式
             yyyy:表示年
             M:表示月,只显示当前月的数字
             MM:表示月份,只显示月份数字,如果数字不足两位补零
             MMM:表示当前月份,即会输出当前月份,也会输入一个"月"字
             MMMM:表示月份,以中文形似显示当前月份,例如:八月
             
             d:表示当月的第几天
             D:表示当年的第几天
             
             E:表示周几
             a:表示上/下午
             
             hh:表示时间,12小时进制
             HH:表示时间,24小时进制
             
             mm:表示分钟
             
             ss:表示秒
             */
            //设置时区
         
            [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:2]];
            [formatter setDateFormat:@"yyyy年MM月dd日 E aHH:mm:ss"];
            
            NSString *timeStr =[formatter stringFromDate:currentDate];
            NSLog(@"%@", timeStr);
            
            [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
    
            //3.设定日期转换格式
            [formatter setDateFormat:@"yyyy年MM月dd日 E aHH:mm:ss"];
    
            NSString *timeStr1 =[formatter stringFromDate:currentDate];
            NSLog(@"%@", timeStr1);
    
            //NSString 转 NSDate
            NSString *str = @"2015年8月20日 11点38分38秒";
            //创建格式转换器
            NSDateFormatter *frmatterStr = [[NSDateFormatter alloc] init];
            
            //设置转换格式,注意,格式必须跟字符串格式一致,否则转换失败
            [frmatterStr setDateFormat:@"yyyy年M月dd日 HH点mm分ss秒"];
            
            //转换
            NSDate *newDate = [frmatterStr dateFromString:str];
            NSLog(@"%@",newDate);
            
        }
        return 0;
    }
  • 相关阅读:
    SpringBoot+MyBatis通过ScriptRunner读取SQL文件
    Redis 分布式锁使用不当,酿成一个重大事故,超卖了100瓶飞天茅台!!!(转)
    better-scroll插件中导致fixed定位失效处理方便
    VUE SSR服务器端渲染NUXT采坑总结
    js的三种异步处理
    微信小程序支付功能讲解
    函数防抖与节流
    转:HTML5 History API 详解
    微信小程序 上拉刷新/下拉加载
    跨域你需要知道这些
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4723626.html
Copyright © 2011-2022 走看看