Jak obliczyć liczbę miesięcy między dwiema datami przy użyciu kakao?Ustalanie liczby miesięcy między dwoma datami przy użyciu kakao
Dzięki, Stan
Jak obliczyć liczbę miesięcy między dwiema datami przy użyciu kakao?Ustalanie liczby miesięcy między dwoma datami przy użyciu kakao
Dzięki, Stan
Spójrz na NSCalendars components:fromDate:toDate:options metody. To pozwala odejmować daty i następnie wyodrębnić miesiącach wartość nieruchomości
NSInteger month = [[[NSCalendar currentCalendar] components: NSCalendarUnitMonth
fromDate: yourFirstDate
toDate: yourSecondDate
options: 0] month];
Składniki: fromdate: todate: opcje nie działa poprawnie na poniższym przykładzie:
rozpocząć datę: 1 stycznia 2012 data koniec : 31 marca 2012 r
Num miesięcy z wykorzystaniem powyższej metody = 2
prawidłowe odpowiedzi powinno być 3 miesiące.
Używam długiej metody obliczeń w następujący sposób: 1. Znajdź liczbę dni w miesiącu początkowym. Dodaj go do części ułamkowej miesiąca. Jeśli pierwszą datą jest początek miesiąca, licz go jako pełny miesiąc. 2. Znajdź liczbę dni w miesiącu końcowym. Dodaj go do części ułamkowej miesiąca. Jeśli data jest ostatnia w miesiącu, licz ją jako pełny miesiąc. 3. Znajdź całe/pełne miesiące między 2 datami i dodaj do całkowitej części miesiąca. 4. Dodaj liczbę całkowitą & część ułamków miesięcy, aby uzyskać prawidłową wartość.
Aby uzyskać odpowiedź, która zawiera część miesiąca dodaje można stosować:
- (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)];
}
miałem obliczyć miesiącach zmiany pomiędzy dwiema datami. Oznacza to, że od 31 stycznia do 1 lutego minął 1 miesiąc: NSCalendar.components
zwróci 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!")
}
}
Dokładnie to, czego potrzebowałem - dzięki. –