时间解析(NSDate、NSCalendar、NSDateComponents):

1、使用NSCalendar和NSDateComponents解析日期,直接获取到年月日时分秒。获取到年月日时分秒其实都是通过NSDateComponents来解析的

[calendar components:(NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
NSDate *date = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comps;

    // 年月日获得
comps = [calendar components:(NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date]; NSInteger y = [comps year]; NSInteger m = [comps month]; NSInteger d = [comps day]; NSLog(@"year: %ld month: %ld, day: %ld", y, m, d);

  

//当前的时分秒获得

comps = [calendar components:(NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond)  fromDate:date];

NSInteger h = [comps hour];

NSInteger m = [comps minute];

NSInteger s = [comps second];

NSLog(@"hour: %li minute: %li second: %li", h, m,s);

  

2、还可以使用NSDateFormatter将日期格式化为 "yyyy-MM-dd hh/HH:mm:ss" 后,再用字符串逐步切割,逐渐获取到年月日时分秒(先用" "切割,然后再用”-“和”:"切割)即可获取到

NSDate *date = [NSDate date];

NSDateFormatter *formatter= [[NSDateFormatteralloc] init];

formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";

NSString *str = [formatterstringFromDate:date];

 NSArray *arr = [str componentsSeparatedByString:@" "];

 NSArray *arr1 = [arr[0] componentsSeparatedByString:@"-"]; // 年 月 日

 NSArray *arr2 = [arr[1] componentsSeparatedByString:@":"]; // 时 分 秒

  

最新文章

  1. maven 打war包tomcat服务器乱码问题
  2. MFC 单文档消息执行顺序。
  3. vs2012 MSDN帮助文档离线包下载安装方法
  4. Hibernate常见问题
  5. 查看oracle数据库中的保留字
  6. leetcode 110 Balanced Binary Tree ----- java
  7. Uva 1588 Kickdown
  8. HW5.21
  9. 学习XAML中的一些基本语法
  10. ANDROID_MARS学习笔记_S05_002_给传感器注册listener
  11. Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志
  12. hdu4085
  13. USACO5.4-Character Recognition
  14. APP测试常用工具以及框架
  15. 队列queue实现线程的消费者和生产者
  16. poj 1151 (未完成) 扫描线 线段树 离散化
  17. maven配置多仓库镜像(转)
  18. react-navigation实现页面框架(转载)
  19. AttributeError: 'WebElement' object has no attribute 'send_keys'
  20. Redis 在线管理工具(phpRedisAdmin)介绍

热门文章

  1. SecureCRT rz 上传文件失败问题
  2. MongoDB的学习--文档的查询
  3. IP Failover Setup using Keepalived on CentOS/Redhat 6
  4. C# 通过Get、Post、Soap调用WebService的方法
  5. String-------RegularHelper
  6. cart中回归树的原理和实现
  7. nodejs+express+jade+mongodb给我baby做个小相册(2)-留言板
  8. Use the PDFs below or the HTML contents to the left to install and configure P6 EPPM and its additional components.
  9. 重构13天 抽取方法对象(Extract Method Object)
  10. 适用于jquery1.11.1的ajaxfileupload.js