博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中的NSdate
阅读量:4452 次
发布时间:2019-06-07

本文共 4062 字,大约阅读时间需要 13 分钟。

@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;}

 

转载于:https://www.cnblogs.com/wohaoxue/p/4723626.html

你可能感兴趣的文章
[QT编程]QT实现的一个渐隐渐显窗体
查看>>
在Web工程中引入Jquery插件报错解决方案
查看>>
大学总结之影响我最深的十本书
查看>>
用myEclipse连接数据源生成动态数据报表
查看>>
[myeclipse]@override报错问题
查看>>
자주 쓰이는 정규표현식
查看>>
超简单的listview单选模式SingleMode(自定义listview item)
查看>>
vue-11-路由嵌套-参数传递-路由高亮
查看>>
HDU 1199 - Color the Ball 离散化
查看>>
[SCOI2005]骑士精神
查看>>
Hibernate原理解析-Hibernate中实体的状态
查看>>
六时车主 App 隐私政策
查看>>
C语言常见问题 如何用Visual Studio编写C语言程序测试
查看>>
Web用户的身份验证及WebApi权限验证流程的设计和实现
查看>>
hdu 2098 分拆素数和
查看>>
[ONTAK2010]Peaks kruskal重构树,主席树
查看>>
ECMAScript6-let与const命令详解
查看>>
iOS 使用系统相机、相册显示中文
查看>>
什么是敏捷设计
查看>>
SCSS的基本操作
查看>>