2011-09-23 7 views
-1

NSDateComponents를 사용하여 수요일과 금요일에 몇 가지 미래의 날짜를 얻는 방법? 답변은 크게 감사하겠습니다. 미리 감사드립니다.미래 날짜를 얻는 방법

+3

(http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

답변

3

완전히 총알 방지를 원한다면 실제로는 약간 까다로운 문제입니다. 여기에 내가 그것을 할 것입니다 방법은 다음과 같습니다 [? 당신이 시도 무엇]

NSInteger wednesday = 4; // Wed is the 4th day of the week (Sunday is 1) 
NSInteger friday = 6; 

NSDate *start = ...; // your starting date 
NSDateComponents *components = [[NSDateComponents alloc] init]; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

for (int i = 0; i < 100; ++i) { 
    [components setDay:i]; 
    NSDate *target = [gregorian dateByAddingComponents:components toDate:start options:0]; 
    NSDateComponents *targetComponents = [gregorian components:NSUIntegerMax fromDate:target]; 
    if ([targetComponents weekday] == wednesday || [targetComponents weekday] == friday) { 
    NSLog(@"found wed/fri on %d-%d-%d", [targetComponents month], [targetComponents day], [targetComponents year]); 
    } 
} 

[gregorian release]; 
[components release]; 
+0

을 그것은! 작동합니다. 고마워. .. – isarathg

0

올바른 요일을 얻으려면 NSCalendar의 적절한 인스턴스를 만들고 dateFromComponents :를 사용하여 NSDate 개체를 만든 다음 components : fromDate :를 사용하여 요일을 검색해야합니다.

NSDate *yourDate = ... 
NSDateComponents* components = [[NSDateComponents alloc] init]; 
components.week = weeks; 
NSDate* futureDate = [[NSCalendar reCurrentCalendar] dateByAddingComponents:components toDate:yourDate options:0]; 
[components release]; 

추신 : 당신은 콘크리트 평일 NSDate 예 (f.e. 수요일)가있는 경우

0

, 당신은 다음과 같은 코드를 사용하여 새로운 미래 날짜를 얻을 수 있습니다 Brian과 동의하고, 묻기 전에 연구 해보십시오.

관련 문제