2012-01-26 12 views
0

土曜日と日曜日の2つの日付の間だけを取得しようとしていますが、なぜ私は週に自由な日を取得するのか分かりません。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; 
+0

NSTimeInterval間隔= i * 24 * 60 * 60; 解決済み! –

+0

質問にあなたの答えを編集しました。あなた自身の質問と回答を編集する能力があります。私は私の編集で少し推論を明らかにした。 –

答えて

2

Here is a link to another answer私は(恥知らずな、私が知っている)に投稿しました。これは、将来の日付であなたを助けるかもしれないいくつかのコードを持っています。このメソッドはNSDateのカテゴリとして実装され、NSDateのメソッドになります。

週末に役立つ機能がいくつかあります。しかし、これら二つが最も役に立つかもしれません:

- (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]; 
} 
+0

ありがとう! –

1

に覚えておくべき1つの非常に重要なことは、1日が(必ずしも)24 * 60 * 60秒に等しいではないということです。 Calendrical Calculationsガイドを参照してくださいNSCalendar– dateByAddingComponents:toDate:options:

を使用します。そして、あなたは自分が

何が本当に少し退屈に思えるかもしれない行う必要があるが、これを行うには、正しいことだ日付の計算を行うべきではありません。

+0

私はあなたにこれに言及することを信じることを意味しました。それは多くの人々によって見過ごされている。 – Jim

関連する問題