8
A
回答
13
看NSCalendars components:fromDate:toDate:options方法。它可以讓你減去日期,然後提取幾個月屬性值
34
NSInteger month = [[[NSCalendar currentCalendar] components: NSCalendarUnitMonth
fromDate: yourFirstDate
toDate: yourSecondDate
options: 0] month];
0
組件:FROM日期:TODATE:選擇不正確的以下示例工作:
開始日期:2012年1月1日 結束日期:3月31日,使用上述方法個月2012
NUM = 2
正確答案應爲3個月。
我用很長的計算方法如下: 1.查找開始月份的天數。將它添加到月份的小數部分。如果第一個日期是月初,請將其計爲整月。 2.找到月底的天數。將其添加到月份的小數部分如果日期是本月的最後一個月,則將其計爲整月。 3.查找2個日期之間的整個/整個月份並添加到月份的整數部分。 4.將月份的小數部分加上整數&以獲得正確的值。
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)];
}
1
我不得不計算兩個日期之間的月變化。這意味着,從一月份的31日至2月1日本月1日已通過了NSCalendar.components
將返回0
import UIKit
func monthsSince(from: NSDate, to: NSDate) -> Int {
let fromComponents = NSCalendar.currentCalendar().components([.Month, .Year], fromDate: from)
let toComponents = NSCalendar.currentCalendar().components([.Month, .Year], fromDate: to)
return ((toComponents.year - fromComponents.year) * 12) + (toComponents.month - fromComponents.month)
}
let tests = [
(from: (day: 1, month: 1, year: 2016), to: (day: 31, month: 1, year: 2016), result: 0),
(from: (day: 22, month: 1, year: 2016), to: (day: 5, month: 2, year: 2016), result: 1),
(from: (day: 22, month: 12, year: 2015), to: (day: 1, month: 1, year: 2016), result: 1),
(from: (day: 1, month: 1, year: 2016), to: (day: 1, month: 2, year: 2016), result: 1),
(from: (day: 1, month: 1, year: 2016), to: (day: 1, month: 3, year: 2016), result: 2)
]
for test in tests {
let from = NSCalendar.currentCalendar().dateWithEra(1, year: test.from.year, month: test.from.month, day: test.from.day, hour: 0, minute: 0, second: 0, nanosecond: 0)!
let to = NSCalendar.currentCalendar().dateWithEra(1, year: test.to.year, month: test.to.month, day: test.to.day, hour: 0, minute: 0, second: 0, nanosecond: 0)!
if monthsSince(from, to: to) == test.result {
print("Test \(test), Passed!")
} else {
print("Test \(test), Failed!")
}
}
相關問題
- 1. 兩個日期之間的月數SAS
- 2. 兩個日期之間的月數
- 3. 獲取天數,並確定從兩個日期之間的月份
- 4. 如何查找兩個日期之間的確切月份和日期 - angularjs
- 5. 數兩個日期之間的幾個月中data.table
- 6. VB.NET確定兩個日期和時間之間的時間差
- 7. 兩個日期之間的月份差異計算lastdate月
- 8. 本月和上個月的兩個日期時間之間的平均時間
- 9. 計算按月分隔的兩個日期之間的天數
- 10. 兩個日期之間的特定日期的數量
- 11. 計算兩個日期之間的完整月數在SQL
- 12. 熊貓 - 兩個日期之間的月數
- 13. TIMESTAMPDIFF在MYSQL中計算兩個日期之間的月數#
- 14. 計算兩個日期之間每月的天數 - SQL
- 15. 計算Excel中兩個日期之間的月數
- 16. 在Android的兩個日期之間差異的(年,月,日)
- 17. 兩個日期之間的星期/月的差異
- 18. Ruby:在兩個日期之間發生的月份特定日期(例如,每月的第一天)的次數
- 19. 在兩個日期之間搜索周/明月記錄計數(數字)和周/月的StartDate + EndDate(日期時間)
- 20. 在兩個給定日期之間創建日期序列 - 二月份失敗
- 21. LINQ到兩個日期之間的SQL夜每個月
- 22. 兩個日期之間的總天數
- 23. 選擇兩個日期在3個月或更短的日期之間
- 24. 熊貓在特定月份的兩個日期之間得到天數
- 25. 使用javascript獲取兩個日期之間的日期列表
- 26. 如何使用PHP計算兩個日期之間的日期?
- 27. 找到第三個星期日每個月給兩個日期之間
- 28. 如何在mysql月份間拆分兩個日期之間的月份
- 29. 特定日期之間一個月明智的日期(從日期爲準)
- 30. 如何確定日期是否在PHP中的兩個日期之間?
正是我需要的 - 謝謝。 – 2010-01-24 21:07:26