0

2 つの日付の間の土曜日と日曜日だけを取得しようとしていますが、週に空き日を取得する理由がわかりません。

これが私のコードです:

- (BOOL)checkForWeekend:(NSDate *)aDate {
    BOOL isWeekendDate = NO;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange weekdayRange = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:aDate];
    NSUInteger weekdayOfDate = [components weekday];

    if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
        // The date falls somewhere on the first or last days of the week.
        isWeekendDate = YES;
    }
    return isWeekendDate;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSString *strDateIni = [NSString stringWithString:@"28-01-2012"];
    NSString *strDateEnd = [NSString stringWithString:@"31-01-2012"];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSDate *startDate = [df dateFromString:strDateIni];
    NSDate *endDate = [df dateFromString:strDateEnd];

    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];

   // int months = [comps month];
    int days = [comps day];

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

        NSTimeInterval interval = i;
        NSDate * futureDate = [startDate dateByAddingTimeInterval:interval];

        BOOL isWeekend = [self checkForWeekend:futureDate]; // Any date can be passed here.

        if (isWeekend) {
            NSLog(@"Weekend date! Yay!");
        }
        else
        {
            NSLog(@"Not is Weekend");
        }


    }

}

問題: 問題の原因NSTimeInterval interval = i; は、for ループのロジックが日単位で反復することでした。時間間隔を i に設定すると、秒単位で反復されました。

NSTimeInterval のドキュメントから

NSTimeInterval は常に秒単位で指定されます。

答え:

NSTimeInterval行を次のように変更します

NSTimeInterval interval = i*24*60*60;
4

2 に答える 2

2

これは私がSOに投稿した別の回答へのリンクです(恥知らずです、私は知っています)。将来の日付に役立つコードがいくつかあります。メソッドは NSDate のカテゴリとして実装されます。つまり、NSDate のメソッドになります。

週末に役立つ機能がいくつかあります。ただし、次の 2 つが最も役立つ可能性があります。

- (NSDate*) theFollowingWeekend;
- (NSDate *) thePreviousWeekend;

受信者 (自己) の前後の週末の日付を返します。

一般に、1 日が 86400 秒であるという概念を使用するべきではなく、NSDateComponents と NSCalendar を使用する必要があります。これは、日付間で夏時間が移行する場合でも機能します。このような:

- (NSDate *) dateByAddingDays:(NSInteger) numberOfDays {
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = numberOfDays;

    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    return [theCalendar dateByAddingComponents:dayComponent toDate:self options:0];
}
于 2012-01-26T22:56:26.957 に答える
1

覚えておくべき非常に重要なことの 1 つは、1 日は (必ずしも) 24*60*60 秒に等しくないということです。また、日付演算を自分で行うべきではありません

本当に必要なことは少し退屈に思えるかもしれませんが、これは正しいことですNSCalendar:– dateByAddingComponents:toDate:options:

暦計算ガイドを参照してください。

于 2012-01-26T22:36:34.900 に答える