Language/Objective-C

월별 날짜 계산 방법(윤년 포함)

아르비스 2011. 4. 29. 15:55
IOS용으로 작성함

data변환을 위한 method

+ (NSString*)dateString:(NSDate*)date format:(NSString*)format {

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];

[fmt setTimeStyle:NSDateFormatterNoStyle];

[fmt setDateFormat:format];

NSString *s = [fmt stringFromDate:date];

[fmt release];

return s;

}


+ (NSArray *)datetimeObject:(NSDate *)stDate

{

    NSString *strDate = [self dateString:stDate format:@"yyyy:MM:dd:HH:mm:SS"];

    NSArray *arrDate = [strDate componentsSeparatedByString:@":"];

    

    if ([arrDate count] == 6) {

        return arrDate;

    }

    

    return nil;

}


+ (NSInteger) valueforDate:(NSDate *)stDate Key:(NSInteger)key

{

    NSInteger result = [[[self datetimeObject:stDate] objectAtIndex:key] intValue];

    

    return result;

}

 
 logical method

+ (NSInteger)LeapYearCalculation:(NSInteger)year

{

    if ((((year%4)==0) && ((year%100)!=0)) || ((year%400)==0)){

        return 29;

    }

    

    return 28;

}


+ (NSInteger)MonthDays:(NSDate *)inDate

{

    NSInteger year = [self valueforDate:inDate Key:0];

    NSInteger month = [self valueforDate:inDate Key:1];

    

    if (month == 2) {

        return [self LeapYearCalculation:year];

    }

    else if(((month%7)%2) == 0) {

        return 30;

    }

    

    return 31;

}

 
end일까지 Month 구하기

+ (NSInteger) totalMonths:(NSDate *)stDate end:(NSDate *)edDate

{

    NSInteger stMonth;

    NSInteger edMonth;

    NSInteger result;

    

    if (!edDate) {

        edDate = [NSDate date];

    }

    

    stMonth = ([[[self datetimeObject:stDate] objectAtIndex:0]intValue] * 12) + ([[[self datetimeObject:stDate] objectAtIndex:1]intValue]);

    edMonth = ([[[self datetimeObject:edDate] objectAtIndex:0]intValue] * 12) + ([[[self datetimeObject:edDate] objectAtIndex:1]intValue]);

    

    result = edMonth - stMonth;

    

    return result;



연도별 일수 구하기

+ (NSInteger)MonthDaysbyStr:(NSInteger)year Month:(NSInteger)month

{

    if (month == 2) {

        return [self LeapYearCalculation:year];

    }

    else if(((month%7)%2) == 0) {

        return 30;

    }

    

    return 31;    

}



+ (NSInteger)yearForDays:(NSInteger)year

{

    NSInteger result = 0;

    

    for (int i = 0; i < 12; i++) {

        result += [self MonthDaysbyStr:year Month:i];

    }

    return result;

}


일자까지 day수 구하기

+ (NSInteger) totalDays:(NSDate *)stDate end:(NSDate *)edDate

{

    NSInteger result = 0;

    

    if (!edDate) {

        edDate = [NSDate date];

    }

    

    if ([edDate compare:stDate] != NSOrderedDescending) {

        return nil;

    }


    NSInteger stYY = [self valueforDate:stDate Key:0];

    NSInteger stMM = [self valueforDate:stDate Key:1];

    NSInteger stDD = [self valueforDate:stDate Key:2];

    

    NSInteger edYY = [self valueforDate:edDate Key:0];

    NSInteger edMM = [self valueforDate:edDate Key:1];

    NSInteger edDD = [self valueforDate:edDate Key:2];

    

    NSInteger gapYear = edYY - stYY;

    

    if (gapYear > 0) {

        // 온전히 1 차이가 나는 경우 각년도별 일자를 더한다.

        if ((gapYear) > 1) {

            for (int idx=1; idx < gapYear; idx++) {

                result += [self yearForDays:(stYY + idx)];

            }

        }

        /// day for start Year

        // stdate next month ~ 12/31

        for (int mm = stMM+1; mm < 13; mm++) {

            result += [self MonthDaysbyStr:stYY Month:mm];

        }

        

        // stdate for day

        result += [self MonthDaysbyStr:stYY Month:stMM] - stDD;

        

        /// day for End Year

        // 1/1~ end before month

        for (int mm = 1; mm < stMM; mm++) {

            result += [self MonthDaysbyStr:edYY Month:mm];

        }

        // The last days

        result += stDD;

    }

    // 1 차이가 안나는 경우 

    // month 

    NSInteger gapMonth = edMM - stMM;

    // 한달이상 차이 나는 경우 

    if (gapMonth > 0) {

        if (gapMonth > 1) {

            for (int mm = stMM+1 ; mm < edMM; mm++) {

                result += [self MonthDaysbyStr:stYY Month:mm];

            }

        }

        

        // add Start days in Month

        result += [self MonthDaysbyStr:stYY Month:stMM] - stDD;

        

        // add End days in Month

        result += edDD;

    }

    

    return result;

}