标签:
main.m
1 #import <Foundation/Foundation.h> 2 #import "NSDate+CalculateDay.h" 3 4 int main(int argc, const char * argv[]) { 5 @autoreleasepool { 6 NSDate *now = [NSDate date]; 7 NSUInteger days = [now daysOfMonth]; 8 NSLog(@"本月的天数=%lu", (unsigned long)days); //本月的天数=31 9 10 days = [now daysOfYear]; 11 NSLog(@"本年的天数=%lu", (unsigned long)days); //本年的天数=365 12 13 NSDate *firstDay = [now firstDayOfMonth]; 14 NSLog(@"firstDayOfMonth=%@", firstDay); //firstDayOfMonth=2015-10-01 11:44:24 +0000 15 16 NSDate *lastDay = [now lastDayOfMonth]; 17 NSLog(@"lastDayOfMonth=%@", lastDay); //lastDayOfMonth=2015-10-31 11:44:24 +0000 18 19 NSDate *newDate = [lastDay addMonthAndDay:0 days:2]; 20 NSLog(@"lastDayOfMonth的后天=%@", newDate); //lastDayOfMonth的后天=2015-11-02 11:44:24 +0000 21 22 newDate = [lastDay addMonthAndDay:2 days:2]; 23 NSLog(@"lastDayOfMonth的2个月零2天后时间=%@", newDate); //lastDayOfMonth的2个月零2天后时间=2016-01-02 11:44:24 +0000 24 25 NSArray *arrMonthAndDay = [firstDay monthAndDayBetweenTwoDates:newDate]; 26 NSLog(@"lastDayOfMonth的2个月零2天后时间和firstDayOfMonth相差(%@个月零%@天)", arrMonthAndDay[0], arrMonthAndDay[1]); //lastDayOfMonth的2个月零2天后时间和firstDayOfMonth相差(3个月零1天) 27 } 28 return 0; 29 }
NSDate+CalculateDay.h
1 #import <Foundation/Foundation.h> 2 3 @interface NSDate (CalculateDay) 4 /** 5 * 获取对应月份的天数 6 * 7 * @return 对应月份的天数 8 */ 9 - (NSUInteger)daysOfMonth; 10 11 /** 12 * 获取对应年份的天数 13 * 14 * @return 对应年份的天数 15 */ 16 - (NSUInteger)daysOfYear; 17 18 /** 19 * 获取对应月份的第一天时间 20 * 21 * @return 对应月份的第一天时间 22 */ 23 - (NSDate *)firstDayOfMonth; 24 25 /** 26 * 获取对应月份的最后一天时间 27 * 28 * @return 对应月份的最后一天时间 29 */ 30 - (NSDate *)lastDayOfMonth; 31 32 /** 33 * 根据月数和天数间隔,获取间隔后的时间 34 * 35 * @param months 月数间隔 36 * @param days 天数间隔 37 * 38 * @return 间隔后的时间 39 */ 40 - (NSDate *)addMonthAndDay:(NSUInteger)months days:(NSUInteger)days; 41 42 /** 43 * 根据开始时间和结束时间,获取间隔的时间数组 44 * 45 * @param toDate 结束时间 46 * 47 * @return 间隔的时间数组(月数和天数;toDate-fromDate的比较值是有符号整数NSInteger,所以存在负数的可能) 48 */ 49 - (NSArray *)monthAndDayBetweenTwoDates:(NSDate *)toDate; 50 51 @end
NSDate+CalculateDay.m
1 #import "NSDate+CalculateDay.h" 2 3 @implementation NSDate (CalculateDay) 4 5 - (NSUInteger)daysOfMonth { 6 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 7 NSRange range = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self]; 8 return range.length; 9 } 10 11 - (NSUInteger)daysOfYear { 12 NSUInteger days = 0; 13 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 14 NSDateComponents *comps = [gregorian components:NSCalendarUnitYear fromDate:self]; 15 16 for (NSUInteger i=1; i<=12; i++) { 17 [comps setMonth:i]; 18 days += [[gregorian dateFromComponents:comps] daysOfMonth]; 19 } 20 return days; 21 } 22 23 - (NSDate *)firstDayOfMonth { 24 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 25 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 26 NSDateComponents *comps = [gregorian components:unitFlags fromDate:self]; 27 [comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //使用UTC或GMT解决时区相差8小时的问题 28 [comps setDay:1]; 29 30 return [gregorian dateFromComponents:comps]; 31 } 32 33 - (NSDate *)lastDayOfMonth { 34 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 35 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 36 NSDateComponents *comps = [gregorian components:unitFlags fromDate:self]; 37 [comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //使用UTC或GMT解决时区相差8小时的问题 38 [comps setDay:[self daysOfMonth]]; 39 40 return [gregorian dateFromComponents:comps]; 41 } 42 43 - (NSDate *)addMonthAndDay:(NSUInteger)months days:(NSUInteger)days { 44 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 45 NSDateComponents *comps = [NSDateComponents new]; 46 [comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //使用UTC或GMT解决时区相差8小时的问题 47 [comps setMonth:months]; 48 [comps setDay:days]; 49 50 return [gregorian dateByAddingComponents:comps toDate:self options:0]; 51 } 52 53 - (NSArray *)monthAndDayBetweenTwoDates:(NSDate *)toDate { 54 NSMutableArray *mArrMonthAndDay = [[NSMutableArray alloc] initWithCapacity:2]; 55 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 56 unsigned unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay; 57 NSDateComponents *comps = [gregorian components:unitFlags fromDate:self toDate:toDate options:0]; 58 59 [mArrMonthAndDay addObject:[NSString stringWithFormat:@"%ld", (long)[comps month]]]; 60 [mArrMonthAndDay addObject:[NSString stringWithFormat:@"%ld", (long)[comps day]]]; 61 return mArrMonthAndDay; 62 } 63 64 @end
结果:
1 2015-10-07 11:44:24.959 OCNSCalendar[790:42164] 本月的天数=31 2 2015-10-07 11:44:24.960 OCNSCalendar[790:42164] 本年的天数=365 3 2015-10-07 11:44:24.967 OCNSCalendar[790:42164] firstDayOfMonth=2015-10-01 11:44:24 +0000 4 2015-10-07 11:44:24.967 OCNSCalendar[790:42164] lastDayOfMonth=2015-10-31 11:44:24 +0000 5 2015-10-07 11:44:24.967 OCNSCalendar[790:42164] lastDayOfMonth的后天=2015-11-02 11:44:24 +0000 6 2015-10-07 11:44:24.967 OCNSCalendar[790:42164] lastDayOfMonth的2个月零2天后时间=2016-01-02 11:44:24 +0000 7 2015-10-07 11:44:24.968 OCNSCalendar[790:42164] lastDayOfMonth的2个月零2天后时间和firstDayOfMonth相差(3个月零1天)
Objective-C语法之NSCalendar的使用(包含NSDateComponents)
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4574013.html