// NSCalendar

// 获取当前日历

NSCalendar *calendar = [NSCalendar currentCalendar];

// 获取当前时间日期的各个值

NSDate *currentDate = [NSDate date];

NSUInteger unitFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond;

NSDateComponents *com = [calendar components:unitFlag fromDate:currentDate];

NSLog(@"year = %i, month = %i, day = %i, hour = %i, minuts=%i, seconds = %i", com.year, com.month, com.day, com.hour, com.minute, com.second);

// 通过各个值获取NSDate

NSCalendar *calendar1 = [NSCalendar autoupdatingCurrentCalendar];

NSDateComponents *settingComs = [[NSDateComponents alloc]init];

settingComs.year = 2014;

settingComs.month = 6;

settingComs.day = 1;

settingComs.hour = 11;

settingComs.minute = 30;

settingComs.second = 0;

NSDate *settingDate = [calendar1 dateFromComponents:settingComs];

NSLog(@"settingDate is : %@", settingDate);

// 计算时间差

NSCalendar *diffCalendar = [NSCalendar currentCalendar];

NSString *str1 = @"2016-07-07 20:20:20";

NSString *str2 = @"2016-07-01 10:10:10";

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

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date1 = [dateFormatter dateFromString:str1];

NSDate *date2 = [dateFormatter dateFromString:str2];

NSUInteger diffCalendarUnitFlag = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitSecond;

NSDateComponents *diffComs = [diffCalendar components:diffCalendarUnitFlag fromDate:date1 toDate:date2 options:0];

NSLog(@"year = %i, month = %i, day = %i, hour = %i, minuts=%i, seconds = %i", diffComs.year, diffComs.month, diffComs.day, diffComs.hour, diffComs.minute, diffComs.second);

NSCalendar NSDateComponents的更多相关文章

  1. ios--NSCalendar NSDateComponents

    原文: ios时间那点事--NSCalendar NSDateComponents http://my.oschina.net/yongbin45/blog/156181 目录[-] iOS时间那点事 ...

  2. NSDate,NSCalendar,NSTimer,NSTimeZone

      NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...

  3. Coding源码学习第二部分(FunctionIntroManager.m)

    接上篇.上篇有一个细节忘了写,在Coding_iOS-Info.plist 里面添加了一个key 是 Status bar is initially hidden  Value 是 YES,在appl ...

  4. iOS 时间处理(转)

    NSDate NSDate对象用来表示一个具体的时间点. NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate存储的是GMT时间,使用的时候会根据 当 ...

  5. NSDate的运算

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...

  6. iOS下日期的处理

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...

  7. iOS下日期的处理(世界标准时转本地时间)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...

  8. ios日期格式转换

    转自:http://blog.csdn.net/l_ch_g/article/details/8217725 1.如何如何将一个字符串如“ 20110826134106”装化为任意的日期时间格式,下面 ...

  9. ios 对日期的处理(包括计算昨天时间、明天时间)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...

随机推荐

  1. 【BZOJ】3211: 花神游历各国

    题意 \(n\)个点,第\(i\)个点值为\(a_i\).\(m\)个询问,每次询问\([l, r]\)内的和或者将\([l, r]\)的每个值改为自己的算术平方根.(\(n \le 100000, ...

  2. [BZOJ2803][Poi2012]Prefixuffix

    2803: [Poi2012]Prefixuffix Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 219  Solved: 95[Submit][St ...

  3. web.xml的一份配置(备忘)

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...

  4. Caused by: java.io.NotSerializableException: com.omhy.common.model.entity.XXX解决方法

    启动tomact时引起的Caused by: java.io.NotSerializableException异常 种种情况就是没有序列化.序列化可以将内存中的类写入文件或数据库中 Serializa ...

  5. zk label控件内容换行

    Label控件本身无法换行,不过div却可以,只要设置了div的宽度,那么就想如果在Label控件外套个div会怎样,结果可喜可乐: <div width="80px"> ...

  6. iPhone6的CSS3媒体查询

    @media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (orientation : po ...

  7. C++复制对象时勿忘每一部分

    现看这样一个程序: void logCall(const string& funcname) //标记记录 { cout <<funcname <<endl; } cl ...

  8. zju(11)在IAR中移植ucos到msp430

    准备材料 1.在TI官网上下载430的固件库,我用的是msp430f5528的板子,下载的是F5xx_F6xx_Core_Lib 地址http://www.ti.com/tool/msp-exp430 ...

  9. jQuery库中的变量$和其它类库的变量$冲突解决方案

    jQuery.noConflict();//把变量$给其它插件 /* 由于把jQuery插件中的变量$给了其它插件使用 那么在调用jQuery插件的时候只能使用jQuery 但是这样很不方便 1.其实 ...

  10. Zabbix自带模板监控MySQL

    先使用主机链接Mysql模板 . 然后写获取mysql状态的脚本,即配合zabbix模板里面的参数的脚本. #!/bin/bash MYSQL_USER='zabbix' #这里为在本地数据库授权的账 ...