main.m
1 #import <Foundation/Foundation.h> 2 /** 3 * 根据输入的时间,获取对应月份的天数 4 * 5 * @param currentDate 输入的时间 6 * 7 * @return 对应月份的天数 8 */ 9 NSUInteger daysOfMonth(NSDate *currentDate) { 10 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 11 NSRange range = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate]; 12 return range.length; 13 } 14 /** 15 * 根据输入的时间,获取对应年份的天数 16 * 17 * @param currentDate 输入的时间 18 * 19 * @return 对应年份的天数 20 */ 21 NSUInteger daysOfYear(NSDate *currentDate) { 22 NSUInteger days = 0; 23 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 24 NSDateComponents *comps = [gregorian components:NSCalendarUnitYear fromDate:currentDate]; 25 26 for (NSUInteger i=1; i<=12; i++) { 27 [comps setMonth:i]; 28 days += daysOfMonth([gregorian dateFromComponents:comps]); 29 } 30 return days; 31 } 32 /** 33 * 根据输入的时间,获取对应月份的第一天时间 34 * 35 * @param currentDate 输入的时间 36 * 37 * @return 对应月份的第一天时间 38 */ 39 NSDate* firstDayOfMonth(NSDate *currentDate) { 40 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 41 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 42 NSDateComponents *comps = [gregorian components:unitFlags fromDate:currentDate]; 43 [comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //使用UTC或GMT解决时区相差8小时的问题 44 [comps setDay:1]; 45 46 NSDate *firstDay = [gregorian dateFromComponents:comps]; 47 return firstDay; 48 } 49 /** 50 * 根据输入的时间,获取对应月份的最后一天时间 51 * 52 * @param currentDate 输入的时间 53 * 54 * @return 对应月份的最后一天时间 55 */ 56 NSDate* lastDayOfMonth(NSDate *currentDate) { 57 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 58 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 59 NSDateComponents *comps = [gregorian components:unitFlags fromDate:currentDate]; 60 [comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //使用UTC或GMT解决时区相差8小时的问题 61 [comps setDay:daysOfMonth(currentDate)]; 62 63 NSDate *lastDay = [gregorian dateFromComponents:comps]; 64 return lastDay; 65 } 66 /** 67 * 根据输入的时间和添加的月数间隔、天数间隔,获取间隔后的时间 68 * 69 * @param currentDate 输入的时间 70 * @param months 月数间隔 71 * @param days 天数间隔 72 * 73 * @return 间隔后的时间 74 */ 75 NSDate* addMonthAndDayToDate(NSDate *currentDate, NSUInteger months, NSUInteger days) { 76 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 77 NSDateComponents *comps = [[NSDateComponents alloc] init]; 78 [comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //使用UTC或GMT解决时区相差8小时的问题 79 [comps setMonth:months]; 80 [comps setDay:days]; 81 82 NSDate *newDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0]; 83 return newDate; 84 } 85 /** 86 * 根据输入的开始时间和结束时间,获取间隔的时间数组(月数和天数;toDate-fromDate的比较值是有符号整数NSInteger,所以存在负数的可能) 87 * 88 * @param fromDate 开始时间 89 * @param toDate 结束时间 90 * 91 * @return 间隔的时间数组(月数和天数;toDate-fromDate的比较值是有符号整数NSInteger,所以存在负数的可能) 92 */ 93 NSArray* monthAndDayBetweenTwoDates(NSDate *fromDate, NSDate *toDate) { 94 NSMutableArray *mArrMonthAndDay = [[NSMutableArray alloc] initWithCapacity:2]; 95 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 96 unsigned unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay; 97 NSDateComponents *comps = [gregorian components:unitFlags fromDate:fromDate toDate:toDate options:0]; 98 99 [mArrMonthAndDay addObject:[NSString stringWithFormat:@"%ld", (long)[comps month]]]; 100 [mArrMonthAndDay addObject:[NSString stringWithFormat:@"%ld", (long)[comps day]]]; 101 return mArrMonthAndDay; 102 } 103 int main(int argc, const char * argv[]) { 104 @autoreleasepool { 105 NSDate *now = [NSDate date]; 106 NSUInteger days = daysOfMonth(now); 107 NSLog(@"本月的天数=%lu", (unsigned long)days); //本月的天数=30 108 109 days = daysOfYear(now); 110 NSLog(@"本年的天数=%lu", (unsigned long)days); //本年的天数=365 111 112 NSDate *firstDay = firstDayOfMonth(now); 113 NSLog(@"firstDayOfMonth=%@", firstDay); //firstDayOfMonth=2015-04-01 00:58:39 +0000 114 115 NSDate *lastDay = lastDayOfMonth(now); 116 NSLog(@"lastDayOfMonth=%@", lastDay); //lastDayOfMonth=2015-04-30 00:58:39 +0000 117 118 NSDate *newDate = addMonthAndDayToDate(lastDay, 0, 2); 119 NSLog(@"lastDayOfMonth的后天=%@", newDate); //lastDayOfMonth的后天=2015-05-02 00:58:39 +0000 120 121 newDate = addMonthAndDayToDate(lastDay, 2, 2); 122 NSLog(@"lastDayOfMonth的2个月零2天后时间=%@", newDate); //lastDayOfMonth的2个月零2天后时间=2015-07-02 00:58:39 +0000 123 124 NSArray *arrMonthAndDay = monthAndDayBetweenTwoDates(firstDay, newDate); 125 NSLog(@"lastDayOfMonth的2个月零2天后时间和firstDayOfMonth相差(%@个月零%@天)", arrMonthAndDay[0], arrMonthAndDay[1]); //lastDayOfMonth的2个月零2天后时间和firstDayOfMonth相差(3个月零1天) 126 } 127 return 0; 128 }
结果:
1 2015-04-30 00:58:39.794 OCNSCalendar[988:83921] 本月的天数=30 2 2015-04-30 00:58:39.795 OCNSCalendar[988:83921] 本年的天数=365 3 2015-04-30 00:58:39.800 OCNSCalendar[988:83921] firstDayOfMonth=2015-04-01 00:58:39 +0000 4 2015-04-30 00:58:39.800 OCNSCalendar[988:83921] lastDayOfMonth=2015-04-30 00:58:39 +0000 5 2015-04-30 00:58:39.800 OCNSCalendar[988:83921] lastDayOfMonth的后天=2015-05-02 00:58:39 +0000 6 2015-04-30 00:58:39.800 OCNSCalendar[988:83921] lastDayOfMonth的2个月零2天后时间=2015-07-02 00:58:39 +0000 7 2015-04-30 00:58:39.800 OCNSCalendar[988:83921] lastDayOfMonth的2个月零2天后时间和firstDayOfMonth相差(3个月零1天)
时间: 2024-11-25 04:10:11