NSCalendar

    /*
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierGregorian;        //公历(经常使用)
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierBuddhist;         //佛教日历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierChinese;          //中国农历(经常使用)
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierCoptic
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierEthiopicAmeteMihret  //
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierEthiopicAmeteAlem //
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierHebrew;           //希伯来日历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierISO8601      //ISO8601(可是如今还不可用)
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierIndian         //印度日历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierIslamic       //伊斯兰历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierIslamicCivil  //伊斯兰教日历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierJapanese     //日本日历(和历,经常使用)
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierPersian //波斯历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierRepublicOfChina  //中华民国日历(台湾)
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierIslamicTabular  //伊斯兰历
     FOUNDATION_EXPORT NSString * const NSCalendarIdentifierIslamicUmmAlQura
     
     

     
     */
    
     //+ currentCalendar
    
    //取得当前用户的逻辑日历
    //currentCalendar取得的值会一直保持在cache中,第一次取得之后若是用户修改该系统日历设定,这个值也不会改变。
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSLog(@"calendar = %@",calendar);
    
    //+ (id)autoupdatingCurrentCalendar
    //取得当前用户的逻辑日历
    //用autoupdatingCurrentCalendar,那么每次取得的值都会是当前系统设置的日历的值。
    NSCalendar *autoupdatingCurrent = [NSCalendar autoupdatingCurrentCalendar];
    NSLog(@"autoupdatingCurrent = %@",autoupdatingCurrent);
    
    
    //- initWithCalendarIdentifier:
    //若是想要用公历的时候,就要将NSDateFormatter的日历设置成公历。不然随着用户的系统设置的改变,取得的日期的格式也会不同。
    NSCalendar *initCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian] ;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setCalendar:initCalendar];
    
   // 这两个类方法都将返回当前客户端的逻辑日历,区别在于:currentCalendar取得的值会一直保持在cache中,第一次用此方法实例化对象后,即便修改了系统日历设定,这个对象也不会改变。而使用autoupdatingCurrentCalendar,当每次修改系统日历设定,其实例化的对象也会随之改变。
    
    
    //NSCalendar经常使用对象方法与类方法:
    
    //1.- setFirstWeekday:
    //设置第一个工做日
    //设定每周的第一天从星期几开始,好比:
    //.  如需设定从星期日开始,则value传入1
    //.  如需设定从星期一开始,则value传入2
    //.  以此类推
    [initCalendar setFirstWeekday:1];
    
    //2.- (void)setLocale:(NSLocale *)locale
   
    [initCalendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSLog(@"%@",initCalendar.locale.localeIdentifier);
    //– setLocale:
    //设置设置区域
    [initCalendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
    
    
    //3.- (void)setTimeZone:(NSTimeZone *)tz
    //设置时区
    [initCalendar setTimeZone:[NSTimeZone defaultTimeZone]];
    
    
    //4. - (void)setMinimumDaysInFirstWeek:(NSUInteger)mdw
    //设定做为(每一年及每个月)第一周必须包含的最少天数,好比:.  如需设定第一周最少包括7天,则value传入7
    //– setMinimumDaysInFirstWeek:
    [initCalendar setMinimumDaysInFirstWeek:7];
    
    //-- Getting Information About a Calendar
    
    //5– calendarIdentifier
    //返回日历标示符
    NSString *calendarIdentifier = [initCalendar calendarIdentifier];
    NSLog(@"calendarIdentifier = %@",calendarIdentifier);
    
    //– firstWeekday
    //返回日历指定的每周的第一天从星期几开始。缺省为星期天,即firstWeekday = 1
    NSUInteger firstWeekday = [initCalendar firstWeekday];
    NSLog(@"firstWeekday = %lu",(unsigned long)firstWeekday);
    
    
    //6. - (NSUInteger)ordinalityOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
    //– locale
    // 返回日历指定的地区信息。
    NSLocale *locale = [initCalendar locale];
    NSLog(@"locale = %@",locale.localeIdentifier);
   
    //– maximumRangeOfUnit:    //返回单元的最大范围
    //- minimumRangeOfUnit:    //返回单元的最小范围
    NSRange range = [initCalendar maximumRangeOfUnit:NSCalendarUnitDay];//好比Day Calendar Unit就是一个月最多31天这个意思
    NSLog(@"range = %lu",(unsigned long)range.length);
    
//    NSEraCalendarUnit -- 纪元单位。对于NSGregorianCalendar(公历)来讲,只有公元前(BC)和公元(AD);而对于其它历法可能有不少,例如日本和历是以每一代君王统治来作计算。
//    NSYearCalendarUnit -- 年单位。值很大,至关于经历了多少年,将来多少年。
//    NSMonthCalendarUnit -- 月单位。范围为1-12
//    NSDayCalendarUnit -- 天单位。范围为1-31
//    NSHourCalendarUnit -- 小时单位。范围为0-24
//    NSMinuteCalendarUnit -- 分钟单位。范围为0-60
//    NSSecondCalendarUnit -- 秒单位。范围为0-60
//    NSWeekCalendarUnit -- 周单位。范围为1-53
//    NSWeekdayCalendarUnit -- 星期单位,每周的7天。范围为1-7
//    NSWeekdayOrdinalCalendarUnit -- 没彻底搞清楚
//    NSQuarterCalendarUnit -- 几刻钟,也就是15分钟。范围为1-4
//    NSWeekOfMonthCalendarUnit -- 月包含的周数。最多为6个周
//    NSWeekOfYearCalendarUnit -- 年包含的周数。最多为53个周
//    NSYearForWeekOfYearCalendarUnit -- 没彻底搞清楚
//    NSTimeZoneCalendarUnit -- 没彻底搞清楚
//    
    //- minimumDaysInFirstWeek
    //返回日历指定的第一周必须包含的最少天数。
    NSInteger minimumDays = [initCalendar minimumDaysInFirstWeek];
    NSLog(@"minimumDays = %ld",(long)minimumDays);
    
    //- ordinalityOfUnit:inUnit:forDate:
    //在一个给定的时间,小日历单元如(一天)在大日历单元(一周)中的序数。
    //好比forDate参数是星期一,并且firstWeekday 参数被设置为2(也就是星期一为一周的第一天),那么返回为1
    //经过这个函数能够判断 例如 :给定的日期是在一周的第几天,或一月的第几周。一年的第几个月。一年的第几天等
    //注:firstWeekday的设定会影响这个函数的返回值
    NSInteger ordinality = [initCalendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekOfYear forDate:[NSDate date]];
    NSLog(@"ordinality = %ld",(long)ordinality);
    
    //- rangeOfUnit:inUnit:forDate:
    //一个小日历单元下。大日历单元的范围 例如 小日历单元是天。大日历单元是周。那么范围就是7天。就是1-7
    NSRange rangeOfUnit = [initCalendar rangeOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekOfYear forDate:[NSDate date]];
    NSLog(@"rangeOfUnit = %lu",(unsigned long)rangeOfUnit.length);
    
    //– rangeOfUnit:startDate:interval:forDate:
    //用于返回日期date(参数)所在的那个日历单元unit(参数)的开始时间(sDate)。
    //其中参数unit指定了日历单元,参数sDate用于返回日历单元的第一天,参数unitSecs用于返回日历单元的长度(以秒为单位),参数date指定了一个特定的日期。
    //若是startDate和interval均为可计算的,那么函数返回YES不然返回NO.好比:
    NSDate *dateOut = [NSDate date];
    //NSDate **sDate = &dateOut;
    
    NSDateComponents *dc = [[NSDateComponents alloc] init];
    [dc setYear: 2013];
    [dc setMonth: 4];
    [dc setDay: 6];
    
    NSDate *date = [calendar dateFromComponents:dc];

    NSTimeInterval intOut;
    NSTimeInterval *intervalOut = &intOut;
    //7.- (BOOL)rangeOfUnit:(NSCalendarUnit)unit startDate:(NSDate **)datep interval:(NSTimeInterval *)tip forDate:(NSDate *)date
    
    //根据参数提供的时间点,返回所在日历单位的开始时间。若是startDate和interval都可以计算,则返回YES;不然返回NOunit -- 日历单位  datep -- 开始时间,经过参数返回 tip -- 日历单位所对应的秒数,经过参数返回  date -- 时间点参数
    if ([initCalendar rangeOfUnit:NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekOfYear startDate:&dateOut interval:intervalOut forDate:date]){
        NSLog(@"%@", [dateOut description]);
        NSLog(@"%f", *intervalOut/60/60/24);
    }
    else{
        NSLog(@"Can not be calculated!");
    }
    
    //- timeZone:
    //返回日历指定的时区信息。
    NSTimeZone *timeZone = [initCalendar timeZone];
    NSLog(@"timeZone = %@",timeZone.abbreviation);
    
    //--
    
    //-- Calendrical Calculations
    
    //- components:fromDate:
    //返回时间组件
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *comps = [initCalendar components:unitFlags fromDate:[NSDate date]];
    NSLog(@"NSDateComponents - %ld",(long)comps.year);
    
    //- components:fromDate:toDate:options:
    //返回时间组件 比较2个日期
    NSDate *startDate = date;
    NSDate *endDate = [NSDate date];
    unsigned int unitFlags2 = NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *comps2 = [initCalendar components:unitFlags2 fromDate:startDate  toDate:endDate  options:0];
    NSInteger months = [comps2 month];
    NSInteger days = [comps2 day];
    NSLog(@"months = %ld days = %ld",(long)months,(long)days);
    
    //- dateByAddingComponents:toDate:options:
    //追加日期 并返回一个新日期
    //
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps3 = [[NSDateComponents alloc] init];
    [comps3 setMonth:2];
    [comps3 setDay:3];
    NSDate *newDate = [initCalendar dateByAddingComponents:comps3 toDate:currentDate  options:0];
    NSLog(@"newDate = %@",newDate);
    
相关文章
相关标签/搜索