2016-08-16 4 views
1

NSDateが現在の週に該当するかどうかを確認するにはどうすればよいですか?私は以下を試してみましたが、先週の日付も"Date is in this week"の状態になっています。NSDateが今週入荷しているかどうかを確認するには?

- (NSInteger)thisW:(NSDate *)date 
{ 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSDateComponents *todaysComponents = 
    [gregorian components:NSWeekCalendarUnit fromDate:[NSDate date]]; 

    NSUInteger todaysWeek = [todaysComponents weekday]; 


    NSDateComponents *otherComponents = 
    [gregorian components:NSWeekCalendarUnit fromDate:date]; 

    NSUInteger datesWeek = [otherComponents weekday]; 
    NSLog(@"Date %@",date); 
    if(todaysWeek==datesWeek){ 
     //NSLog(@"Date is in this week"); 
     return 1; 
    }else if(todaysWeek+1==datesWeek){ 
     //NSLog(@"Date is in next week"); 
     return 2; 
    } else { 
     return 0; 
    } 

} 
+0

NSWeekCalendarUnitを交換しなければなりませんでした。日曜日、月曜日など – Avi

+0

これを参考にしてください[iOS SDKの週番号を取得する](http://stackoverflow.com/questions/17587697/get-the-week-number-in-ios-sdk) –

答えて

1

私は `weekday`プロパティは、曜日を与えるNSCalendarUnitWeekOfYear

- (NSInteger)thisW:(NSDate *)date 
{ 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *todaysComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:[NSDate date]]; 
    NSUInteger todaysWeek = [todaysComponents weekOfYear]; 
    NSDateComponents *otherComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:date]; 
    NSUInteger datesWeek = [otherComponents weekOfYear]; 

    //NSLog(@"Date %@",date); 
    if(todaysWeek==datesWeek){ 
     //NSLog(@"Date is in this week"); 
     return 1; 
    }else if(todaysWeek+1==datesWeek){ 
     //NSLog(@"Date is in next week"); 
     return 2; 
    } else { 
     return 0; 
    } 

} 
0

週の数を取得するために、この機能を使用します。

このコードを使用して
- (NSInteger)numberOfWeek:(NSDate *)date 
{ 
    NSCalendar *calender = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [calender components:NSCalendarUnitWeekOfYear fromDate:date]; 
    return [components weekOfYear]; 
} 

を、あなたはこのように一週間を比較することができます。私はちょうど比較のためにdateWithTimeIntervalSinceNow:を使用して来週の日付を使用しています。

if ([self numberOfWeek:[NSDate date]] == [self numberOfWeek:[NSDate dateWithTimeIntervalSinceNow: (60.0f*60.0f*24.0f*7.0f)]]) 
{ 
    NSLog(@"Same Week"); 
} 
else 
{ 
    NSLog(@"Different Week"); 
} 
0

最初に現在の日付の曜日と曜日を取得します。今すぐあなたの興味日付

BOOL isInCurrentWeek = [self date:your given date isBetweenDate:firstDayOfTheWeek andDate:lastDayOfTheWeek]; 
NSLog(@"%d",isInCurrentWeek); 

期待する方法

- (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate 
{ 
    if ([date compare:beginDate] == NSOrderedAscending) 
     return NO; 

    if ([date compare:endDate] == NSOrderedDescending) 
     return NO; 

    return YES; 
} 
0

開始日と間隔を計算する方法NSCalendarrangeOfUnit:startDate:interval:forDateを用いて溶液を入れて怒鳴る指定されたメソッドを使用し

NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *currentComps = [myCalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; 


[currentComps setWeekday:1]; // 1: sunday 
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps]; 
[currentComps setWeekday:7]; // 7: saturday 
NSDate *lastDayOfTheWeek = [myCalendar dateFromComponents:currentComps]; 

今週の

- (BOOL)isInCurrentWeek:(NSDate *)date 
{ 
    NSDate *startDate = nil; 
    NSTimeInterval interval = 0.0; 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    // calendar.firstWeekday = 1; set the index of the first weekday if necessary 
    [calendar rangeOfUnit:NSWeekOfYearCalendarUnit startDate: &startDate interval: &interval forDate: [NSDate date]]; 
    NSDate *endDate = [calendar dateByAddingUnit:NSSecondCalendarUnit value: (NSInteger)interval toDate: startDate options:NSCalendarMatchNextTime]; 
    return [date compare:startDate] == NSOrderedDescending && [date compare:endDate] == NSOrderedAscending; 
} 

またはスウィフト

func isInCurrentWeek(date : NSDate) -> Bool 
{ 
    var startDate : NSDate? 
    var interval : NSTimeInterval = 0.0 
    let calendar = NSCalendar.currentCalendar() 
// calendar.firstWeekday = 1 set the index of the first weekday if necessary 
    calendar.rangeOfUnit(.WeekOfYear, startDate: &startDate , interval: &interval, forDate: NSDate()) 
    let endDate = calendar.dateByAddingUnit(.Second, value: Int(interval), toDate: startDate!, options: .MatchNextTime)! 
    return date.compare(startDate!) == .OrderedDescending && date.compare(endDate) == .OrderedAscending 
} 
関連する問題