NSCalendar

206 阅读4分钟
NSCalendar 对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。可以很方便的用来表示日期,获取日期的各种信息,包括年、月、日,时分秒。可以很方便的计算两个日期之前的关系。也可以很方便的根据一个日期获取另一个日期。

NSCalendar -- 日历类,它提供了大部分的日期计算接口,并且允许您在NSDateNSDateComponents之间转换
NSTimeZone -- 时区信息
NSLocale -- 本地化信息
NSDate -- 表示一个绝对的时间点
NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类
NSDateFormatter -- 用来在日期和字符串之间转换
1. 日历的创建
根据提供的日历标示符初始化。
identifier 的范围可以是:
        NSCalendarIdentifierGregorian         公历
        NSCalendarIdentifierBuddhist          fojiao日历
        NSCalendarIdentifierChinese           中国农历
        NSCalendarIdentifierHebrew             日历
        NSCalendarIdentifierIslamic            日历
        NSCalendarIdentifierIslamicCivil        日历
        NSCalendarIdentifierJapanese          日本日历
        NSCalendarIdentifierRepublicOfChina  (台湾)
        NSCalendarIdentifierPersian           波斯历
        NSCalendarIdentifierIndian            印度日历
        NSCalendarIdentifierISO8601           ISO8601
// 使用用户手机设置的日期信息,有缓存,用户手机日历改变后不会变
@property (class, readonly, copy) NSCalendar *currentCalendar;  
// 使用用户手机设置的日期信息,并且用户改变之后会跟着改变
@property (class, readonly, strong) NSCalendar *autoupdatingCurrentCalendar API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // tracks changes to user's preferred calendar identifier
// 使用指定的标识获取日期,比如农历、佛历,常用的是格里高利历(NSCalendarIdentifierGregorian)
+ (nullable NSCalendar *)calendarWithIdentifier:(NSCalendarIdentifier)calendarIdentifierConstant API_AVAILABLE(macos(10.9), ios(8.0), watchos(2.0), tvos(9.0));
- (nullable id)initWithCalendarIdentifier:(NSCalendarIdentifier)ident NS_DESIGNATED_INITIALIZER;

NSCalendar中有一个重要的概念NSCalendarUnit,这是一个位枚举,意味着作为参数可以采用位运算的方式传参。
另外一个比较重要的类是NSDateComponents,上面是通过位参数获取每个单位(年月日)的信息,返回的结构可以看到是一个NSDateComponents,如果我要表示一个日期的信息,构建日期或者进行日期的计算,就少不了NSDateComponents,它可以将日期按照单位的形式封装起来,然后通过NSCalendar的方法进行计算
NSDateComponents *comps = [NSCalendar.currentCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];

2. 日历的设置
NSCalendar *calendar = [NSCalendar currentCalendar];
    
NSString *calendarIdentifier =  calendar.calendarIdentifier;// 获取日历标示符
    
NSString *localeIdentifier = calendar.locale.localeIdentifier;// 获取地区信息
NSString *localeIdentifier2 = [calendar.locale objectForKey:NSLocaleIdentifier];//语言地区
 
NSTimeZone *timeZone = calendar.timeZone; // 获取时区信息
      
NSUInteger firstWeekday = calendar.firstWeekday;// 获取每周的第一天从星期几开始    缺省为星期天
        
NSUInteger minimumDaysInFirstWeek = calendar.minimumDaysInFirstWeek;// 获取第一周必须包含的最少天数  缺省为 1
3. 日历信息的获取
1) 获取一个小的单位在一个大的单位里面的序数
NSUInteger count = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:[NSDate date]];
(NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date;

NSCalendarUnit包含的值有:
  NSCalendarUnitEra                 -- 纪元单位。对于 NSGregorianCalendar (公历)来说,只有公元前(BC)和公元(AD);
                                       而对于其它历法可能有很多,例如日本和历是以每一代君王统治来做计算。
  NSCalendarUnitYear                -- 年单位。值很大,相当于经历了多少年,未来多少年。
  NSCalendarUnitMonth               -- 月单位。范围为1-12
  NSCalendarUnitDay                 -- 天单位。范围为1-31
  NSCalendarUnitHour                -- 小时单位。范围为0-24
  NSCalendarUnitMinute              -- 分钟单位。范围为0-60
  NSCalendarUnitSecond              -- 秒单位。范围为0-60
  NSCalendarUnitWeekOfMonth / NSCalendarUnitWeekOfYear -- 周单位。范围为1-53
  NSCalendarUnitWeekday             -- 星期单位,每周的7天。范围为1-7
  NSCalendarUnitWeekdayOrdinal      -- 没完全搞清楚
  NSCalendarUnitQuarter             -- 几刻钟,也就是15分钟。范围为1-4
  NSCalendarUnitWeekOfMonth         -- 月包含的周数。最多为6个周
  NSCalendarUnitWeekOfYear          -- 年包含的周数。最多为53个周
  NSCalendarUnitYearForWeekOfYear   -- 没完全搞清楚
  NSCalendarUnitTimeZone            -- 没完全搞清楚
 - (NSTimeInterval)timeIntervalSinceNow;
 以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔

- (NSTimeInterval)timeIntervalSince1970;
以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔

- (NSTimeInterval)timeIntervalSinceReferenceDate;
以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔

#import "ViewController.h"

static const NSCalendarUnit unitFlags = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal);

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // NSDateComponent 可以获得日期的详细信息,即日期的组成
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    NSLog(@"%ld",(long)components.month);
}

@end