`

ios NSDate NSCalendar

    博客分类:
  • ios
 
阅读更多

NSCalendar + NSDateComponents

  1. 历法能使人类确定每一日再无限的时间中的确切位置并记录历史。
  2. 日历,历法,一般历法都是遵循固定的规则的,具有周期性。日历都是已知的或可预测的。
  3. 任何一种具体的历法,首先必须明确规定起始点,即开始计算的年代,这叫“纪元”;以及规定一年的开端,这叫“岁首”。此外,还要规定每年所含的日数,如何划分月份,每月有多少天等等。
  4. NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。
  5. NSDateFomatter表示的时间默认以公历(即阳历)为参考,可以通过设置calendar属性变量获得特定历法下的时间表示。
  6. NSDate是独立与任何历法的,它只是时间相对于某个时间点的时间差;NSDate是进行日历计算的基础。
  7. NSDateComponents将时间表示成适合人类阅读和使用的方式,通过NSDateComponents可以快速而简单地获取某个时间 点对应的“年”,“月”,“日”,“时”,“分”,“秒”,“周”等信息。当然一旦涉及了年月日时分秒就要和某个历法绑定,因此 NSDateComponents必须和NSCalendar一起使用,默认为公历。
  8. NSDateComponents除了像上面说的表示一个时间点外,还可以表示时间段,例如:两周,三个月,20年,7天,10分钟,50秒等等。时间段用于日历的计算,例如:获取当前历法下,三个月前的某个时间点。
  9. 可以说,要获取某个时间点在某个历法下的表示,需要NSDateComponents;要计算当前时间点在某个历法下对应的一个时间段前或后的时间点,需要NSDateComponents。
  10. NSDateComponents返回的day, week, weekday, month, year这一类数据都是从1开始的。因为日历是给人看的,不是给计算机看的,从0开始就是个错误。

NSDateComponents实例化的方式

第一种:
01 //  先定义一个遵循某个历法的日历对象
02 NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
03 //  通过已定义的日历对象,获取某个时间点的NSDateComponents表示,并设置需要表示哪些信息(NSYearCalendarUnit, NSMonthCalendarUnit, NSDayCalendarUnit等)
04 NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
05 NSLog(@"year(年份): %i", dateComponents.year);
06 NSLog(@"quarter(季度):%i", dateComponents.quarter);
07 NSLog(@"month(月份):%i", dateComponents.month);
08 NSLog(@"day(日期):%i", dateComponents.day);
09 NSLog(@"hour(小时):%i", dateComponents.hour);
10 NSLog(@"minute(分钟):%i", dateComponents.minute);
11 NSLog(@"second(秒):%i", dateComponents.second);
12  
13 //  Sunday:1, Monday:2, Tuesday:3, Wednesday:4, Friday:5, Saturday:6
14 NSLog(@"weekday(星期):%i", dateComponents.weekday);
15  
16 //  苹果官方不推荐使用week
17 NSLog(@"week(该年第几周):%i", dateComponents.week);
18 NSLog(@"weekOfYear(该年第几周):%i", dateComponents.weekOfYear);
19 NSLog(@"weekOfMonth(该月第几周):%i", dateComponents.weekOfMonth);

若获取dateComponents对象时,设置components的时候未添加 NSYearCalendarUnit,dateComponents.year将返回错误的数值,其他的也一样,所以使用 NSDateComponents表示时间时,要确保需要使用的数据都在componets中添加了。

第二种:
01 //  先定义一个遵循某个历法的日历对象
02 NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
03  
04 //  定义一个NSDateComponents对象,设置一个时间点
05 NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
06 [dateComponentsForDate setDay:6];
07 [dateComponentsForDate setMonth:5];
08 [dateComponentsForDate setYear:2004];
09  
10 //  根据设置的dateComponentsForDate获取历法中与之对应的时间点
11 //  这里的时分秒会使用NSDateComponents中规定的默认数值,一般为0或1。
12 NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dc];
13  
14  
15  
16  
17 //  定义一个NSDateComponents对象,设置一个时间段
18 NSDateComponents *dateComponentsAsTimeQantum = [[NSDateComponents alloc] init];
19 [dateComponentsForDate setDay:6];
20  
21 //  在当前历法下,获取6天后的时间点
22 NSDate *dateFromDateComponentsAsTimeQantum = [greCalendar dateByAddingComponents:dateComponentsAsTimeQantum toDate:[NSDate date] options:0];

第三种:
1 //  先定义一个遵循某个历法的日历对象
2 NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
3  
4 //  根据两个时间点,定义NSDateComponents对象,从而获取这两个时间点的时差
5 NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate date] options:0];
6 NSLog(@"number of years:%i", dateComponents.year);

NSCalendar中比较重要的方法和概念

firstWeekday

firstWeekday是大家比较容易浑淆的东西。

大家在使用dateComponents.weekday获取某天对应的星期时,会发现,星期日对应的值为1,星期一对应的值为2,星期二对应的值 为3,依次递推,星期六对应的值为7,这与我们平时理解的方式不一样。然后,我们就开始找是不是可以设置这种对应关系。终于,我们在NSCalendar 中发现了firstWeekday这个变量,从字面意思上看貌似就是我们寻找的那个东西。可是,设置过firstWeekday后,我们又发现完全没有作 用,真是郁闷啊!其实,大家不必郁闷,因为郁闷也没用,iOS中规定的就是周日为1,周一为2,周二为3,周三为4,周四为5,周五为6,周六为7,无法 通过某个设置改变这个事实的,只能在使用的时候注意一下这个规则了。那firstWeekday是干什么用的呢?大家设置一下firstWeekday, 再获取一下dateComponents.weekOfYear或dateComponents.weekOfMonth,看看返回的数据是否发生了变 化。firstWeekday的作用确实是修改当前历法中周的起始位置,但是不能修改周日对应的数值,只能修改一年或一个月中周的数量,以及周的次序。

-(NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;

答疑解惑:
  1. Unit:单元

  2. NSRange:

    typedef struct _NSRange {

    1 NSUInteger location;
    2 NSUInteger length;

    } NSRange;

我们大致可以理解为:某个时间点所在的“小单元”,在“大单元”中的数量(返回值range的location属性变量的值一般是错误的)。例如:

1 //  当前时间对应的月份中有几天
2     [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]].length;
3     //  当前时间对应的月份中有几周(前面说到的firstWeekday会影响到这个结果)
4     [[NSCalendar currentCalendar] rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]].length;

-(NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;

我们大致可以理解为:某个时间点所在的“小单元”,在“大单元”中的位置(从1开始)。例如:

1 //  当前时间对应的周是当前年中的第几周
2     [[NSCalendar currentCalendar] ordinalityOfUnit:NSWeekOfYearCalendarUnit inUnit:NSYearCalendarUnit forDate:self];
3     [[NSCalendar currentCalendar] ordinalityOfUnit:NSWeekCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
4  
5     //  当前时间对应的周是当前月中的第几周
6     [[NSCalendar currentCalendar] ordinalityOfUnit:NSWeekOfMonthCalendarUnit inUnit:NSYearCalendarUnit forDate:self];
7     [[NSCalendar currentCalendar] ordinalityOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];

在这里:NSWeekOfYearCalendarUnit, NSWeekOfMonthCalendarUnit与NSWeekCalendarUnit的使用结果相同,为了避免浑淆,建议在此处使用 NSWeekCalendar,而定义NSDateComponents时使用NSWeekOfYearCalendarUnit和 NSWeekOfMonthCalendarUnit。

-(BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate *)datep interval:(NSTimeInterval )tip forDate:(NSDate *)date;

我们大致可以理解为:“某个时间点”所在的“单元”的起始时间,以及起始时间距离“某个时间点”的时差(单位秒)。例如:

01 NSDate *startDateOfYear;
02     NSDate *startDateOfMonth;
03     NSDate *startDateOfWeek;
04     NSDate *startDateOfDay;
05     NSTimeInterval TIOfYear;
06     NSTimeInterval TIOfMonth;
07     NSTimeInterval TIOfWeek;
08     NSTimeInterval TIOfDay;
09     [[NSCalendar currentCalendar] rangeOfUnit:NSYearCalendarUnit startDate:&startDateOfYear interval:&TIOfYear forDate:[NSDate date]];
10     [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDateOfMonth interval:&TIOfMonth forDate:[NSDate date]];
11     [[NSCalendar currentCalendar] rangeOfUnit:NSWeekCalendarUnit startDate:&startDateOfWeek interval:&TIOfWeek forDate:[NSDate date]];
12     [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startDateOfDay interval:&TIOfDay forDate:[NSDate date]];
13     NSLog(@"firstDateOfYear:%@, FirstDateOfMonth:%@, FirstDateOfWeek:%@, FirstDateOfDay:%@", startDateOfYear, startDateOfMonth, startDateOfWeek, startDateOfDay);
14     NSLog(@"TIOfYear:%f, TIOfMonth:%f, TIOfWeek:%f, TIOfDay:%f", TIOfYear, TIOfMonth, TIOfWeek, TIOfDay);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics