NSCalendar NSDateComponents
// 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的更多相关文章
- ios--NSCalendar NSDateComponents
原文: ios时间那点事--NSCalendar NSDateComponents http://my.oschina.net/yongbin45/blog/156181 目录[-] iOS时间那点事 ...
- NSDate,NSCalendar,NSTimer,NSTimeZone
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...
- Coding源码学习第二部分(FunctionIntroManager.m)
接上篇.上篇有一个细节忘了写,在Coding_iOS-Info.plist 里面添加了一个key 是 Status bar is initially hidden Value 是 YES,在appl ...
- iOS 时间处理(转)
NSDate NSDate对象用来表示一个具体的时间点. NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate存储的是GMT时间,使用的时候会根据 当 ...
- NSDate的运算
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...
- iOS下日期的处理
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...
- iOS下日期的处理(世界标准时转本地时间)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...
- ios日期格式转换
转自:http://blog.csdn.net/l_ch_g/article/details/8217725 1.如何如何将一个字符串如“ 20110826134106”装化为任意的日期时间格式,下面 ...
- ios 对日期的处理(包括计算昨天时间、明天时间)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...
随机推荐
- Codeforces Round #216 (Div. 2) B. Valera and Contest
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
- 《深入浅出Windows 10通用应用开发》
<深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...
- 【BZOJ】2879: [Noi2012]美食节
题意 \(m\)个厨师,\(n\)种菜,每种菜需要做\(p_i\)份,每个厨师做第\(i\)种菜用时\(t_{i, j}\).一个厨师做完一道菜才能做下一道.每份菜的时间是这个厨师做完这道菜的用时加上 ...
- 基于SVG的JS地图插件
一:D3(Data-Driven Documents) 官网地址:http://d3js.org/ 功能非常强大(不支持IE8) D3 是最流行的可视化库之一,它被很多其他的表格插件所使用.它允许绑定 ...
- Bone Collector(01背包)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/N 题目: Description Many year ...
- javascript模块化应用
这是一篇关于js模块化历程的长长的流水账,记录js模块化思想的诞生与变迁,展望ES6模块化标准的未来.经历过这段历史的人或许会感到沧桑,没经历过的人也应该知道这段历史. 无模块时代 在ajax还未提出 ...
- css小记(3)
1.只有块级元素可以设置背景图,行内元素要变成块级元素才可以:2.盒子不设置宽度,只设置高度,默认为100%,并自适应屏幕大小变化,为这个盒子设置margin可以在保证margin效果的同时默认调整盒 ...
- bzoj4457: 游戏任务--双层01背包
这题和NOIP的金明的预算方案(?)很像,只不过附件的数量增多了 如果对主件进行一次01背包,再套一层附件的01背包O(n4)肯定会爆.. 所以我们可以先预处理出,对于每个主件,花的时间为k的情况下, ...
- CentOS下IP的配置
1.打开命令窗口,切换成root账户:su - root 2.进入目录:/etc/sysconfig/network-scripts,打开文件vi ifcfg-eth0 3.修改参数: ## 名称DE ...
- 想通过加HINT让其走全表扫描
一个SQL,通过SPM固定它的执行计划,可以通过DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE实现.也可以通地此功能在不修改原SQL的情况下对其加HINT来固定执行计划.D ...