2014-11-11 2 views
-1

나는 학생 일정을 작성 중이며 문제가 있습니다. 나는 현재 주와 다음주의 배열을 가질 필요가있다. 그래서 만약 오늘 11 월 11 일 화요일이라면 나는 어떻게 든 할 필요가있다. 10.11 화요일 11.11 화요일 12.11 목요일 13.11 금요일 14.11과 다음 주 같은 날에 날짜가있다. 이 기능은 사용자가 요일을 선택하고 오늘 일정을 잡을 수있을 때 내 프로그램에서해야하는 기능입니다. 나는 스택 오버 플로우를 보았지만 모든 솔루션은 정확하지 않았습니다. 너 나 좀 도와 줄 수있어? 여기 내 시도 중 일부입니다.주중 일주일에

NSDate *now = [NSDate date]; 

NSCalendar *calendar = [NSCalendar currentCalendar]; 

NSDate *beginningOfThisWeek; 
NSDate *beginningOfNextWeek; 

NSTimeInterval durationOfWeek; 
[calendar setFirstWeekday:3]; 
[calendar rangeOfUnit:NSCalendarUnitWeekOfMonth 
      startDate:&beginningOfThisWeek 
      interval:&durationOfWeek 
       forDate:now]; 

beginningOfNextWeek = [beginningOfThisWeek dateByAddingTimeInterval:durationOfWeek]; 


NSDate *date = beginningOfThisWeek; 
NSMutableArray *dateArray = [@[] mutableCopy]; 

while (![beginningOfNextWeek compare:date] == NSOrderedSame) { 
    [dateArray addObject:date]; 
    NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:date]; 
    comps.day +=1; 
    date = [calendar dateFromComponents:comps]; 

} 
NSLog(@"%@",dateArray); 
NSLog(@"%@",now); 

하지만 로그에 내가 뭔가 얻을 :

 
2014-11-11 18:37:29.479 StudentSchedule[3964:436389] (
    "2014-11-10 22:00:00 +0000", 
    "2014-11-11 22:00:00 +0000", 
    "2014-11-12 22:00:00 +0000", 
    "2014-11-13 22:00:00 +0000", 
    "2014-11-14 22:00:00 +0000", 
    "2014-11-15 22:00:00 +0000", 
    "2014-11-16 22:00:00 +0000" 
내가 날짜가

하지만, 7, 그리고 난 매우 중요 평일의 이름이 없습니다. 제 질문은 매우 간단하다는 것을 압니다. 그러나 나는 올바른 대답을 찾는 것에 지쳐 있습니다.

+0

'dateByAddingTimeInterval'을 사용하기 전에'durationOfWeek'에서 48 시간을 뺍니다. –

+0

어떻게해야합니까? 2 NSTimeInterval을 추가 하시겠습니까? – SpencerReid

+0

'NSTimeInterval'은'double'의 또 다른 이름입니다. 몇 초를 나타냅니다. '48 * 60 * 60'을 빼면, 약 2 일을 빼서 일주일이 끝날 때 올바른 달력 날짜에 도착하게됩니다. –

답변

0

좋아요, 그러니 특정 날에 앱을여십시오.

그 주에 대한 모든 근무일 (월요일 - 금요일)과 다음 주에 대한 모든 근무일을 원합니다.

오른쪽 ... 몇 가지 기능이 여기에있다

- (void)getDays 
{ 
    NSArray *days = @[]; 

    NSDate *currentDate = [NSDate date]; 

    // add this week's work days to array 
    days = [days arrayByAddingObjectsFromArray:[self workDaysOfWeekFromDate:currentDate]]; 

    NSDate *nextWeek = [self dateWeekAfterDate:currentDate]; 

    // add next week's work days to array 
    days = [days arrayByAddingObjectsFromArray:[self workDaysOfWeekFromDate:nextWeek]]; 
} 

- (NSDate *)dateWeekAfterDate:(NSDate *)date 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateComponents *components = [NSDateComponents new]; 
    components.day = 7; 

    return [calendar dateByAddingComponents:components toDate:date options:0]; 
} 

- (NSInteger)dayOfWeekFromDate:(NSDate *)date 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    NSDateComponents *components = [calendar components:NSCalendarWeekdayUnit fromDate:date]; 

    return components.weekday; 
} 

- (NSDate *)dayWithIndex:(NSInteger)dayIndex fromWeekOfDate:(NSDate *)date 
{ 
    NSInteger dayOfWeekFromDate = [self dayOfWeekFromDate:date]; 

    NSInteger difference = dayIndex - dayOfWeekFromDate; 

    NSDateComponents *components = [NSDateComponents new]; 
    components.day = difference; 

    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    return [calendar dateByAddingComponents:components toDate:date options:0]; 
} 

- (NSArray *)workDaysOfWeekFromDate:(NSDate *)date 
{ 
    // Sunday = 1 
    // ... 
    // Saturday = 7 

    return @[ 
     [self dayWithIndex:2 fromWeekOfDate:date], // Monday 
     [self dayWithIndex:3 fromWeekOfDate:date], // Tuesday 
     [self dayWithIndex:4 fromWeekOfDate:date], // Wednesday 
     [self dayWithIndex:5 fromWeekOfDate:date], // Thursday 
     [self dayWithIndex:6 fromWeekOfDate:date] // Friday 
    ];  
} 

. 모든 일이 발생하고 getDays 함수에서 완료됩니다. 이 작업이 끝나면 days 배열에는 이번 주와 다음 주에 월요일 - 금요일 동안 NSDate 객체가 포함됩니다.

물론 currentDate을 원하는 날짜로 변경할 수 있습니다. 항상 currentDate에서 주일이 걸리고 currentDate 이후에 주가 걸릴 것입니다.

이 항상 일정과 함께 일을하고 윤년 것이다는 등 ...

은 당신이 할 수있는 날짜의 이름을 ... 점점

NSDate *date = [NSDate date]; 

NSDateFormatter *df = [NSDateFormatter new]; 
df.dateFormat = @"EEEE"; 

NSString *dayName = [df stringFromDate:date]; 

형식 EEEE은 하루 수단 이름. 오늘 dayName 문자열은 Tuesday입니다.

EEEE = Tuesday 
EEE = Tue 

당연히 더 적은 수의 줄을 하나의 함수로 병합 할 수 있습니다. 하지만 나는 읽을 수있는 코드를 선호한다.

+0

그것은 나를 위해 상당히 복잡합니다. 나는 당신의 코드를 더 잘 이해하지만 NSLog를 볼 때 배열 "days"에 무엇이 있는지는 비어 있습니다. 내가 뭘 잘못하고 있니? – SpencerReid

+0

@SpencerReid 어디에서 log 문을 넣고 있습니까? – Fogmeister

+0

@SpencerReid 예, 날짜가 어렵습니다. 그래서 당신은 24 * 60 * 60을 사용할 수 없습니다. – Fogmeister