開始日と間隔を計算する方法NSCalendar
のrangeOfUnit: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
}
で
NSWeekCalendarUnit
を交換しなければなりませんでした。日曜日、月曜日など – Aviこれを参考にしてください[iOS SDKの週番号を取得する](http://stackoverflow.com/questions/17587697/get-the-week-number-in-ios-sdk) –