131,NSCalendar的应用
2015-12-23 12:55
246 查看
#import <Foundation/Foundation.h>
int main(int argc,
const char * argv[]) {
//1,使用NSCalendar获取年,月,日,时,分,秒
NSDate *now = [NSDate
date];
NSLog(@"now = %@",now);
//创建日历对象
//components:需要获取哪一些值
NSCalendar *calendar = [NSCalendar
currentCalendar];
NSCalendarUnit type =
NSCalendarUnitYear|kCFCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *cmp = [calendar
components:type fromDate:now];
NSLog(@"year = %ld",cmp.year);
NSLog(@"month = %ld",cmp.month);
NSLog(@"day = %ld",cmp.day);
NSLog(@"hour = %ld",cmp.hour);
NSLog(@"month = %ld",cmp.month);
NSLog(@"second = %ld",cmp.second);
//2,使用NSCalendar
比较两个时间差
NSString *str =
@"2015-11-11 12:12:12 +0000";
NSDateFormatter *formatter = [[NSDateFormatter
alloc]init];
formatter.dateFormat =
@"yyyy-MM-dd HH:mm:ss Z";
NSDate *passDate1 = [formatter
dateFromString:str];
NSLog(@"passDate = %@",passDate1);
NSDate *now1 = [NSDate
date];
NSCalendar *calendar1 = [NSCalendar
currentCalendar];
NSCalendarUnit type1 =
NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *cmp1 = [calendar1
components:type1 fromDate:passDate1
toDate:now1 options:0];
NSLog(@"passDate1 = %@",passDate1);
NSLog(@"now1 = %@",now1);
NSLog(@"差值:%ld-%ld-%ld %ld:%ld:%ld",cmp1.year,cmp1.month,cmp1.day,cmp1.hour,cmp1.minute,cmp1.second);
return 0;
}
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] now = 2015-12-23 04:53:10 +0000
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] year = 2015
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] month = 12
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] day = 23
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] hour = 12
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] month = 12
//2015-12-23 12:53:10.306 13,NSCalendar的应用[1864:188372] second = 10
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372] passDate = 2015-11-11 12:12:12 +0000
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372] passDate1 = 2015-11-11 12:12:12 +0000
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372] now1 = 2015-12-23 04:53:10 +0000
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372]
差值:0-1-11 16:40:58
//Program ended with exit code: 0
int main(int argc,
const char * argv[]) {
//1,使用NSCalendar获取年,月,日,时,分,秒
NSDate *now = [NSDate
date];
NSLog(@"now = %@",now);
//创建日历对象
//components:需要获取哪一些值
NSCalendar *calendar = [NSCalendar
currentCalendar];
NSCalendarUnit type =
NSCalendarUnitYear|kCFCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *cmp = [calendar
components:type fromDate:now];
NSLog(@"year = %ld",cmp.year);
NSLog(@"month = %ld",cmp.month);
NSLog(@"day = %ld",cmp.day);
NSLog(@"hour = %ld",cmp.hour);
NSLog(@"month = %ld",cmp.month);
NSLog(@"second = %ld",cmp.second);
//2,使用NSCalendar
比较两个时间差
NSString *str =
@"2015-11-11 12:12:12 +0000";
NSDateFormatter *formatter = [[NSDateFormatter
alloc]init];
formatter.dateFormat =
@"yyyy-MM-dd HH:mm:ss Z";
NSDate *passDate1 = [formatter
dateFromString:str];
NSLog(@"passDate = %@",passDate1);
NSDate *now1 = [NSDate
date];
NSCalendar *calendar1 = [NSCalendar
currentCalendar];
NSCalendarUnit type1 =
NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *cmp1 = [calendar1
components:type1 fromDate:passDate1
toDate:now1 options:0];
NSLog(@"passDate1 = %@",passDate1);
NSLog(@"now1 = %@",now1);
NSLog(@"差值:%ld-%ld-%ld %ld:%ld:%ld",cmp1.year,cmp1.month,cmp1.day,cmp1.hour,cmp1.minute,cmp1.second);
return 0;
}
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] now = 2015-12-23 04:53:10 +0000
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] year = 2015
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] month = 12
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] day = 23
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] hour = 12
//2015-12-23 12:53:10.305 13,NSCalendar的应用[1864:188372] month = 12
//2015-12-23 12:53:10.306 13,NSCalendar的应用[1864:188372] second = 10
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372] passDate = 2015-11-11 12:12:12 +0000
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372] passDate1 = 2015-11-11 12:12:12 +0000
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372] now1 = 2015-12-23 04:53:10 +0000
//2015-12-23 12:53:10.307 13,NSCalendar的应用[1864:188372]
差值:0-1-11 16:40:58
//Program ended with exit code: 0
相关文章推荐
- 【PostgreSQL】PostgreSQL常用备份方法
- caffe make的时候出错:src/caffe/util/math_functions.cu(140): error: calling a host function("std::signbit
- PHP 命名空间以及自动加载(自动调用的函数,来include文件)
- Chain Codes and Your Brain 链码和大脑
- SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇日期格式转化报400错误解决方法
- Mac上安装NDK
- sqllite3命令行下的常用命令及接口
- ehcache缓存的使用
- Introduction to Chain Codes 链码导论
- jquery实现点击其他区域时隐藏下拉div和遮罩层的方法
- online_judge_1140
- 机房收费系统安装过程
- 排序算法—选择排序算法分析与实现(Python)
- Google 推出的 31 套在线课程
- -bash: netstat: command not found
- Leetcode: Palindrome Permutation II
- LCS最长公共子序列入门题
- Freeman Chain Code链码
- 使用ExpandableListView实现一个时光轴
- [Java]Java工程师成神之路