NSDateComponents Class Reference

        NSDateComponents是NSCalendar的辅助工具类,当你使用NSCalendar提取NSDate的小时数九需要它。

关于NSDate的编程介绍,详见《NSDate Class Reference》
关于NSCalendar的编程介绍详见《 NSCalendar Class Reference》

获取时间属性

date Property
calendar Property
timeZone Property

#pragma mark 获取时间属性
- (void)testGettingDateValue {

    NSCalendar *calendar = [NSCalendar currentCalendar]; // 系统语言环境
    NSDate *date = [NSDate date];// 当前系统时间
    NSTimeZone *timeZone = calendar.timeZone;// 时区

    // 根据时区提取所有数据
    NSDateComponents *dateComponents = [calendar componentsInTimeZone:timeZone fromDate:date];
    // 获取NSDate
    date = dateComponents.date;
    // 获取NSCalendar
    calendar = dateComponents.calendar;
    // 获取NSTimeZone
    timeZone = dateComponents.timeZone;

}

 

验证日期

validDate Property
- isValidDateInCalendar:

#pragma mark 验证日期
- (void)testValidatingDateValue {

    NSCalendar *calendar = [NSCalendar currentCalendar]; // 系统语言环境
    NSDate *date = [NSDate date];// 当前系统时间
    NSTimeZone *timeZone = calendar.timeZone;
    NSDateComponents *dateComponents = [calendar componentsInTimeZone:timeZone fromDate:date];

    // 能否生成日期
    BOOL isValidDate = dateComponents.validDate;
    // 日期是否存在于日历中,以及判断NSTimeZone是否存在日历中
    isValidDate = [dateComponents isValidDateInCalendar:calendar];

}

 

日期组件

era Property
year Property
month Property
day Property
hour Property
minute Property
second Property
nanosecond Property
weekday Property
weekdayOrdinal Property
quarter Property
weekOfMonth Property
weekOfYear Property
yearForWeekOfYear Property
leapMonth Property
- valueForComponent:
- setValue:forComponent:

#pragma mark 日期组件
- (void)testDateComponents {

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *date = [NSDate date];
    NSTimeZone *timeZone = calendar.timeZone;
    NSDateComponents *dateComponents = [calendar componentsInTimeZone:timeZone fromDate:date];
        NSLog(@"%@", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterFullStyle]);

    NSInteger component = dateComponents.era; // 公元前、公元
    component = dateComponents.year; // 年
    component = dateComponents.month; // 月
    component = dateComponents.day; // 日
    component = dateComponents.hour; // 时
    component = dateComponents.minute; // 分
    component = dateComponents.second; // 秒
    component = dateComponents.nanosecond; // 纳秒
    component = dateComponents.weekday; // 周几
    component = dateComponents.weekdayOrdinal; // 工作日的序数
    component = dateComponents.quarter;
    component = dateComponents.weekOfMonth;// 这一月的第几周
    component = dateComponents.weekOfYear;// 这一年的第几周
    component = dateComponents.yearForWeekOfYear; // 年
    component = dateComponents.leapMonth;

    // 通过NSCalendarUnit获取值
    component = [dateComponents valueForComponent:NSCalendarUnitEra];
    // 通过NSCalendarUnit设置值
    [dateComponents setValue:component forComponent:NSCalendarUnitEra];

}

 

其他

参考资料

NSDateComponents Class Reference
Date and Time Programming Guide

 

文档修改记录

时间 描述
2015-10-19 根据IOS9的NSDateComponents API总结

 

版权所有:http://blog.csdn.net/y550918116j

你可能感兴趣的:(ios,Class,NSDate,NSCalendar)