날짜에 값을 추가하거나 날짜간에 차이가 발생하는 데 문제가 있습니다. 계산 된 날짜와 구성 요소가 올바르지 않습니다.dateByAddingComponents 및 NSDateComponents와 날짜의 차이 가져 오기
추가 할 때 1.5 개월을 더하면 1 개월 밖에 걸리지 않습니다. 그러나 모든 정수 ie (1 또는 2 또는 3 등)를 추가하면 올바르게 계산됩니다. 내가 정확히 일년 (위와 거의 동일한 코드)로 추가 된 날짜가있는 경우 차이가 지금
Float32 addAmount = 1.5;
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:addAmount];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:Date1 options:0];
은 올바르게 추가되지만 차이가 계산 될 때, I 0 년 11 개월 얻을 그리고 30 일.
NSDate *startDate = Date1;
NSDate *endDate = Date2;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:startDate
toDate:endDate options:0];
NSInteger years = [components year];
NSInteger months = [components month];
NSInteger days = [components day];
내가 뭘 잘못하고 있니? 또한 kCFCalendarComponentsWrap constanct 추가 및 차이 기능을 모두 있지만 차이가있는 옵션을 추가했습니다.
감사
I 1.5 개월을 추가하면, 난 단지 내가 어떤 정수 즉, 추가 그러나 경우 1 개월 얻을 , 추가 그래서
나는 메모리 누수를 발견하지 못했습니다. 감사합니다. 날짜 변수는 대개 date1 = self.dob.date와 같은 코드가있는 uidatepicker에서 작성됩니다. 여기서 dob는 datepicker 이름입니다. 변수 이름에 관해서는 당신이 절대적으로 옳았습니다. 케이스를 생각하지 않고 게시하도록 변경했습니다. – Rob