1.获取当前时间的年月日时分秒
    /*
    // 获取当前时间
    NSDate *now = [NSDate date];
    NSLog(@"now = %@", now);
    // 日历
    NSCalendar *calendar1 = [NSCalendar currentCalendar];
    // 利用日历类从当前时间对象中获取 年月日时分秒(单独获取出来)
    // components: 参数的含义是, 问你需要获取什么?
    // 一般情况下如果一个方法接收一个参数, 这个参数是是一个枚举 , 那么可以通过|符号, 连接多个枚举值
    NSCalendarUnit type = NSCalendarUnitYear |
                          NSCalendarUnitMonth |
                          NSCalendarUnitDay |
                          NSCalendarUnitHour |
                         NSCalendarUnitMinute |
                        NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar1 components:type fromDate:now];
    NSLog(@"year = %ld", cmps.year);
    NSLog(@"month = %ld", cmps.month);
    NSLog(@"day = %ld", cmps.day);
    NSLog(@"hour = %ld", cmps.hour);
    NSLog(@"minute = %ld", cmps.minute);
    NSLog(@"second = %ld", cmps.second);
     */

2.比较两个时间之间的差值, 比较相差多少年多少月多少日多少小时多少分钟多少秒
    
    // 2.1过去的一个时间
    NSString *str = @"2015-06-29 07:05:26 +0000";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
    NSDate *date = [formatter dateFromString:str];
    // 2.2当前的时间
    NSDate *now = [NSDate date];
    
    NSLog(@"date = %@", date);
    NSLog(@"now = %@", now);
    
    // 2.3比较两个时间
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit type = NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:type fromDate:date toDate:now options:0];
    NSLog(@"%ld年%ld月%ld日%ld小时%ld分钟%ld秒钟", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);

NSCalendar的更多相关文章

  1. 利用NSCalendar类实现日期的比较

    在项目中日期的显示经常会当天的显示时分,当月的显示日时和分,以此类推,难免会涉及到日期的比较,下面介绍一下日期比较的两种方法 比较日期有两种方法 一种是通过系统的NSCalendar类实现 NSStr ...

  2. 用NSCalendar和UICollectionView自定义日历,并实现签到显示

    前一段时间因为工作需要实现了一个可以签到的日历,来记录一下实现的思路 效果如图:   这里的基本需求是: 1,显示用户某个月的签到情况,已经签到的日子打个圈,没有签到且在某个时间范围内的可以签到,其他 ...

  3. Foundation框架下的常用类:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver

    ========================== Foundation框架下的常用类 ========================== 一.[NSNumber] [注]像int.float.c ...

  4. supersr--时间显示逻辑-->NSDate+NSCalendar

    一种:时间逻辑: - (NSString *)created_at{ //    从后台返回的字符串格式:Mon Aug 03 09:17:31 +0800 2014, //NSDateFormatt ...

  5. NSCalendar NSDateComponents

    // NSCalendar // 获取当前日历 NSCalendar *calendar = [NSCalendar currentCalendar]; // 获取当前时间日期的各个值 NSDate ...

  6. iOS NSDate、NSCalendar、NSDateComponents

    时间解析(NSDate.NSCalendar.NSDateComponents): 1.使用NSCalendar和NSDateComponents解析日期,直接获取到年月日时分秒.获取到年月日时分秒其 ...

  7. iOS - OC NSCalendar 日历

    前言 @interface NSCalendar : NSObject <NSCopying, NSSecureCoding> NSCalendar 对世界上现存的常用的历法进行了封装,既 ...

  8. iOS - Swift NSCalendar 日历

    前言 public class NSCalendar : NSObject, NSCopying, NSSecureCoding NSCalendar 对世界上现存的常用的历法进行了封装,既提供了不同 ...

  9. Cocoa Foundation框架学习笔记 - NSCalendar

    + (void)beginTest { /* FOUNDATION_EXPORT NSString * const NSGregorianCalendar; //公历(常用) FOUNDATION_E ...

随机推荐

  1. FireDac 与数据库连接时字符集及对应的字段类型问题

    近日在一个过程调用时发生一个奇怪现象, 异常返回意思是说, 数据的长度是[6], 而字段定义的长度是[3].  分析后认为:  调用过程你不涉及到对返回数据集的字段手动定义问题, 出现这个问题应是两边 ...

  2. JAVA学习方法

    我觉得要想学好的话,首先,你得会用,能玩的起来,然后才会慢慢激发你的兴趣,它为何这么用?就这样一步一步的去探索,不断地积累,总结.编程讲究的是思维,死记硬背是没有意义的,想学好编程就一个字:敲,俩字 ...

  3. C#操作CSV存取类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. Binary Tree Level Order Traversal II

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  5. Oracle之Union与Union all的区别

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...

  6. ectouch第八讲 之模板内容修改

    前台:1.前台页面logo代码[ 文件位置:\mobile\themes\default\index.dwt] <div style="text-align: center;paddi ...

  7. sql server 快捷键

    书签:清除所有书签. CTRL-SHIFT-F2 书签:插入或删除书签(切换). CTRL+F2 书签:移动到下一个书签. F2 功能键 书签:移动到上一个书签. SHIFT+F2 取消查询. ALT ...

  8. Linux用户组与用户组进阶命令

    1.用户锁定 : passwd -l user1 2.解除用户锁定:passwd -u user1 3.用户无密码登记:passwd -d user1 4.添加到附属用户组:gpasswd -a us ...

  9. python中列表和字典常用方法和函数

    Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...

  10. 通过spring工厂读取property配置文件

    /** * Created by ywq on 2016/6/30. */ @Named public class PropertyConfig { private static AbstractBe ...