与时间相关NSCalendar、NSDate类的基本使用
注释dateFormate yyyy:年
MM:月
dd:日 HH:24小时 hh:12小时
mm:分钟 ss:秒钟
Z:时区
1.解决获取NSDate的时间不对
NSDate *nowDate = [NSDate date];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
NSInteger ti = [localTimeZone secondsFromGMTForDate:nowDate];
NSDate *newDate = [nowDate dateByAddingTimeInterval:ti];
NSLog(@"世界统一时间:%@ 当地时间:%@", nowDate, newDate);
1.获取当前时间的年月日时分秒
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:unit fromDate:[NSDate date]];
NSLog(@"年:%ld 月:%ld 日:%ld 时:%ld 分:%ld 秒:%ld",(long)components.year, (long)components.month, (long)components.day, (long)components.hour, (long)components.minute, (long)components.second);
2.比较两个时间之间的差值
NSDate *date1 = [NSDate date]; // 当前的日期
NSString *dateStr = @"2015-06-29 07:05:26 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
NSDate *date2 = [formatter dateFromString:dateStr]; // 指定的日期
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:unit fromDate:date1 toDate:date2 options:0];
NSLog(@"相差年:%ld 月:%ld 日:%ld 时:%ld 分:%ld 秒:%ld",(long)components.year, (long)components.month, (long)components.day, (long)components.hour, (long)components.minute, (long)components.second);
3.获取当前日期的那天在所在月份是星期几
方法1:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.year = 2016;
components.month = 1;
components.day = 4;
NSDate *date = [calendar dateFromComponents:components];
NSUInteger index = [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:[NSDate date]];
NSLog(@"--%lu", index);
注释1:代表星期日 2:代表星期一 3:代表星期二 4:代表星期三 5:代表星期四 6:代表星期五 7:代表星期六
方法2:(好像不行)
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekOfMonth fromDate:[NSDate date]];
NSLog(@"---%ld", (long)components.weekOfMonth);
转载于:https://www.cnblogs.com/dixuexiongying/p/5096874.html
与时间相关NSCalendar、NSDate类的基本使用相关推荐
- NSDate 类的总结,全面基础
NSDate 类的总结,全面基础 <span style="font-size:24px;"><span style="font-size:18px;& ...
- NSCalendar 日历类
Cocoa中对日期和时间的处理 NSCalendar (一) NSCalendar用于处理时间相关问题.比如比较时间前后.计算日期所的周别等. 1. 创建或初始化可用以下方法 + (id)curren ...
- 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...
- 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone) -- IOS(实例)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...
- 日历,日期类(copy)
NSDate *date=[NSDate date];//格林尼治时间NSLog(@"%@",date);NSCalendar *calendar=[NSCalendar curr ...
- iOS - OC NSCalendar 日历iOS - OC NSCalendar 日历
本文目录 前言 1.日历的创建 2.日历的设置 3.日历设置信息的获取 4.日历信息的获取 回到顶部 前言 @interface NSCalendar : NSObject <NSCopying ...
- iOS - OC NSCalendar 日历
前言 @interface NSCalendar : NSObject <NSCopying, NSSecureCoding> NSCalendar 对世界上现存的常用的历法进行了封装,既 ...
- 关于NSCalendar 日历的学习记录
系统中定义的日历有: NSString * const NSGregorianCalendar; 公历(常用) NSString * const NSBuddhistCalendar; 佛教日历 NS ...
- 日期 日历的一些常用类和方法
http://www.cnblogs.com/wayne23/archive/2013/03/25/2981009.html http://zcw-java.iteye.com/blog/189994 ...
最新文章
- Selenium高亮页面对象
- 利刃 MVVMLight 7:命令深入
- 排序算法系列:插入排序算法
- mysql datetime转date_一个MySQL时间戳精度引发的血案
- phpmyadmin登录远程mysql数据库
- bootstrap --- 面板
- 在laravel5.8中集成swoole组件----用协程实现的服务端和客户端(nginx配置篇章)
- win7系统怎么拷贝到u盘_win7/10系统复制文件到u盘提示文件过大怎么办
- 阿里发起“0账期”倡议:新增700万网友提前收货
- 单片机TM4C123学习(一):GPIO模块的应用
- vs2013连接数据库
- VS2017社区版+OpenCV3.4.10配置
- 高中数学立体几何知识点总结(八大定理)
- 依赖注入:语法糖胜于功能组合
- SQL Server 2014 企业版安装教程
- 看完此文,告诉你什么是黑中介
- 5个月的精华:Spring/SpringBoot扩展点手册:手册在手,编码无忧:全网独一份 - 第451篇
- 数据脱敏的场景与价值【总结】
- 斐波那契问题(兔子问题)
- OUC2022秋季软件工程第16组第二周作业
热门文章
- 7-4 输出最小公倍数 (9 分)
- SQL报错:Column count doesn‘t match value count at row 1
- Spring应用上下文 - - context
- 前端类名优秀命名例子_这是一篇需要花费你15分钟阅读的干货!浅谈前端工程化...
- 计算机片段教学优秀教案,精彩教学片段100例—导入篇(1)
- Wait Event Disk file operations I/O
- C++反汇编第五讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式....
- sql 嵌套while需要注意的问题
- 设置UIImage的边框和圆角大小以及颜色
- 一次性从linux发送多个文件到windows