1

1年に24回の支払いがある場合(つまり、1か月に2回)の支払いスケジュールを決定しようとしています。支払いは常に1日と15日になるとは限りません。特定の日から始めて、そこから月2回の支払いを計算する必要があります。

NSCalendarとNSDateComponentsには、整数の日、週、月などを追加する機能しかありません。私が実際に行う必要があるのは、0.5か月を追加できることですが、整数のみを処理します。月の日数が異なるため、15日を追加することはできません(これは、NSCalendarが処理することになっている魔法の一部です)。その月の最初の支払いは毎月の特定の日に行う必要があります。したがって、支払いが8日に始まる場合、すべての奇数の支払いは8日に、すべての偶数の支払いは8日+半月になります。そこで複雑さが発生します。少なくとも、月の加算を使用して、奇数の支払いをすべて取得できると思います。それらの偶数の支払いの日付を取得することは難しい部分です。

私が行方不明になっているNSCalendarを介して半月を追加する方法はありますか?そうでない場合は、これらの24の日付を年間生成するためのエレガントなソリューションがありますか?

4

2 に答える 2

2

さて、開始日から 1 か月後の日付を計算できれば、半月を計算するのに 1 区分しかありません。

NSTimeInterval oneMonthInterval = [oneMonthLater timeIntervalSinceDate:startDate];
NSTimeInterval halfMonthInterval = oneMonthInterval / 2.0;
NSDate *halfAMonthLater = [startDate dateByAddingTimeInterval:halfMonthInterval];
于 2011-04-08T20:52:37.660 に答える
2

いいえ、半月計算は自分で作成する必要があります。ただの楽しみのために、私はあなたのために以下を書きました。楽しみ。

- (NSArray *)createPaymentScheduleWithStartDate:(NSDate *)startDate numberOfPayments:(NSUInteger)payments {
    NSUInteger count = 0;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSMutableArray *schedule = [NSMutableArray arrayWithCapacity:payments];

    // break down the start date for our future payments
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit 
            | NSTimeZoneCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *comps = [cal components:unitFlags fromDate:startDate];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSInteger year = [comps year];


    // For simplicity sake, adjust day larger than 28
    // this way we don't have to test each time we create a date
    // if it valid.
    if (day > 28)
        day = 28;

    // NSDate *a = startDate;
    // because we want all payments to have the same time
    // create the first again
    NSDate *a = [cal dateFromComponents:comps];

    do {
        month++;
        if (month > 12) {
            year++;
            month = 1;
        }

        // create the next month payment date
        comps.month = month;
        comps.day = day;
        comps.year = year;
        NSDate *b = [cal dateFromComponents:comps];

        // find the middle date
        NSTimeInterval m = [b timeIntervalSinceDate:a];
        NSDate *c = [a dateByAddingTimeInterval:m / 2];

        // add dates to our schedule array
        [schedule addObject:a];
        [schedule addObject:c];
        count += 2;

        // adjust to next group of payments
        a = b;

    } while (count < (payments + 5));



    // because we add two payments at a time,
    // we have to adjust that extra payment
    if ([schedule count] > payments) {
        // create range of our excess payments
        NSRange range = NSMakeRange(payments, [schedule count] - payments);
        [schedule removeObjectsInRange:range];
    }

    NSLog(@"Schedule: %@", schedule);
    return [NSArray arrayWithArray:schedule];
}
于 2011-04-09T03:52:29.090 に答える