NSDateFormatter 和 NSDateComponents 的用法
来源:互联网 发布:2016宽带网络那家好 编辑:程序博客网 时间:2024/06/06 13:12
NSDateFormatter调整时间格式的代码
在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理。
例如:
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//用[NSDate date]可以获取系统当前时间
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
//输出格式为:2010-10-27 10:22:13
NSLog(@”%@”,currentDateStr);
//alloc后对不使用的对象别忘了release
[dateFormatter release];
NSDateComponents
NSDateComponents封装在一个可扩展的,面向对象的方式的日期组件。它是用来弥补时间的日期和时间组件提供一个指定日期:小时,分钟,秒,日,月,年,等等。它也可以用来指定的时间,例如,5小时16分钟。一个NSDateComponents对象不需要定义所有组件领域。当一个NSDateComponents的新实例被创建,日期组件被设置为NSUndefinedDateComponent。
一个NSDateComponents对象本身是毫无意义的;你需要知道它是针对什么日历解释,你需要知道它的值是否是正整数和值是多少。
NSDateComponents的实例不负责回答关于一个日期以外的信息,它是需要先初始化的。例如,如果你初始化一个对象为2004年5月6日,其星期几NSUndefinedDateComponent,不是星期四。要得到正确的星期几,你必须创建一个NSCalendar日历实例,创建一个NSDate对象并使用dateFromComponents:方法,然后使用components:fromDate:检索平周几
Getting Information About an NSDateComponents Object
获取一个NSDateComponents对象的信息
- – era 时代
- – year 年
- – month 月
- – day 天
- – hour 时
- – minute 分
- – second 秒
- – week
- – weekday
- – weekdayOrdinal
- – quarter 季度
Setting Information for an NSDateComponents Object
设置一个NSDateComponents对象的信息
- – setEra:
- – setYear:
- – setMonth:
- – setDay:
- – setHour:
- – setMinute:
- – setSecond:
- – setWeek:
- – setWeekday:
- – setWeekdayOrdinal:
- – setQuarter:
例子如下:获得2004年5月6日是星期几
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
NSDateComponents *weekdayComponents =
[gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
NSLog(@"%d",weekday);
如果需要更加详细,请查阅 “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.日期和时间的程序指南
//一段例子:计算距离某一天还有多少时间
NSDate* toDate = [ [ NSDate alloc] initWithString:@"2012-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDate alloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags =
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [cps hour];
NSInteger diffMin = [cps minute];
NSInteger diffSec = [cps second];
NSInteger diffDay = [cps day];
NSInteger diffMon = [cps month];
NSInteger diffYear = [cps year];
NSLog( @" From Now to %@, diff: Years: %d Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );
[toDate release];
[startDate release];
[chineseClendar release];
- NSDateFormatter 和 NSDateComponents 的用法
- NSDateFormatter 和 NSDateComponents 的用法
- NSDateFormatter 和 NSDateComponents 的用法
- NSDateFormatter 和 NSDateComponents 的用法
- IOS NSDateFormatter 和NSDateComponents
- NSDateFormatter NSDate NSCalendar NSdateComponents
- NSDate , NSDateComponents , NSDateFormatter, NSCalendar
- ios开发系列之NSDate,NSDateFormatter,NSDateComponents和NSCalendar
- 日期和时间 NSDate NSTimeInterval NSCalendar NSDateComponents NSDateFormatter
- NSDate NSDateFormatter NSTimeZone NSDateComponents NSCalendar
- NSDateFormatter的用法
- NSDateFormatter的用法
- Foundation框架浅析 - 时间相关的类:NSDate、NSTimeZone、NSLocale、NSDateComponents、NSCalendar、NSDateFormatter
- NSDateFormatter NSDateComponents iphone 时间格式详解
- NSDateFormatter NSDateComponents iphone 时间格式详解
- iOS-NSDateFormatter NSDateComponents iphone 时间格式详解
- 时间解析(NSDate、NSDateFormatter、NSCalendar、NSDateComponents):
- NSDate、NSTimeInterval、NSDateFormatter、NSLocale 、NSTimeZone、NSDateComponents详解
- 网络搭建
- 安装memcached
- 委托、事件与Observer设计模式
- LINUX httpd挂死解决
- java基础知识-构造函数
- NSDateFormatter 和 NSDateComponents 的用法
- 四种工作流的比较
- [排序]SORT QSORT
- spring bean标签,及其子标签使用
- 初中英语基本句型45例 !你忘木忘!小试牛刀,一起翻译!! TBD
- log4j配置
- Type-safe comparision between "operator new/delete" and "allocator"
- 80X86学习笔记--数据传送指令
- C#下ref和out关键字使用