如何使用Cocoa计算两个日期之间的月数?
谢谢, 斯坦
答案 0 :(得分:37)
NSInteger month = [[[NSCalendar currentCalendar] components: NSCalendarUnitMonth
fromDate: yourFirstDate
toDate: yourSecondDate
options: 0] month];
答案 1 :(得分:13)
查看NSCalendars components:fromDate:toDate:options方法。它允许您减去日期,然后提取月份属性值
答案 2 :(得分:5)
要获得包含一个月份的答案,可以使用以下内容:
- (NSNumber *)numberOfMonthsBetweenFirstDate:(NSDate *)firstDate secondDate:(NSDate *)secondDate {
if ([firstDate compare:secondDate] == NSOrderedDescending) {
return nil;
}
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *firstDateComponents = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:firstDate];
NSInteger firstDay = [firstDateComponents day];
NSRange rangeOfFirstMonth = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDate];
NSUInteger numberOfDaysInFirstMonth = rangeOfFirstMonth.length;
CGFloat firstMonthFraction = (CGFloat)(numberOfDaysInFirstMonth - firstDay) / (CGFloat)numberOfDaysInFirstMonth;
// last month component
NSDateComponents *lastDateComponents = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:secondDate];
NSInteger lastDay = [lastDateComponents day];
NSRange rangeOfLastMonth = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:secondDate];
NSUInteger numberOfDaysInLastMonth = rangeOfLastMonth.length;
CGFloat lastMonthFraction = (CGFloat)(lastDay) / (CGFloat)numberOfDaysInLastMonth;
// Check if the two dates are within the same month
if (firstDateComponents.month == lastDateComponents.month
&& firstDateComponents.year == lastDateComponents.year) {
NSDateComponents *dayComponents = [calendar components:NSDayCalendarUnit
fromDate:firstDate
toDate:secondDate
options:0];
NSRange rangeOfMonth = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDate];
NSUInteger numberOfDaysInMonth = rangeOfMonth.length;
return [NSNumber numberWithFloat:(CGFloat)dayComponents.day / (CGFloat)numberOfDaysInMonth];
}
// Start date of the first complete month
NSDateComponents *firstDateFirstDayOfNextMonth = firstDateComponents;
firstDateFirstDayOfNextMonth.month +=1;
firstDateFirstDayOfNextMonth.day = 1;
// First day of the last month
NSDateComponents *secondDateFirstDayOfMonth = lastDateComponents;
secondDateFirstDayOfMonth.day = 1;
NSInteger numberOfMonths = secondDateFirstDayOfMonth.month - firstDateFirstDayOfNextMonth.month
+ (secondDateFirstDayOfMonth.year - firstDateFirstDayOfNextMonth.year) * 12;
return [NSNumber numberWithFloat:(firstMonthFraction + numberOfMonths + lastMonthFraction)];
}
答案 3 :(得分:2)
我必须计算两个日期之间的月份变化。这意味着从1月31日到2月1日已过去,MyConnection.retrieve_connection
将返回0。
NSCalendar.components
答案 4 :(得分:0)
组件:fromDate:toDate:以下示例中的选项无效:
开始日期:2012年1月1日 截止日期:2012年3月31日。
使用上述方法的数月= 2
正确的答案应该是3个月。
我使用的计算方法如下: 1.查找开始月份的天数。将其添加到该月的小部分。如果第一个日期是月初,则将其计为完整月份。 2.找到结束月份的天数。将它添加到月份的小部分如果日期是月份的最后一个,则将其计为整月。 3.查找两个日期之间的整个/完整月份,并添加到该月的整数部分。 4.添加整数&为了获得正确的价值,可以在几个月的时间里分数。
答案 5 :(得分:0)
以下是为iOS11更新的@eliocs答案的objective-c版本:
- (NSInteger)monthsSince:(NSDate *)from to:(NSDate *)to {
NSDateComponents *fromComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:from];
NSDateComponents *toComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:to];
return ((toComponents.year - fromComponents.year) * 12) + (toComponents.month - fromComponents.month);
}