检查NSDate是在本周还是下周

时间:2012-04-13 15:30:36

标签: iphone ios nsdate

有一种方法可以检查NSDate是本周还是下周?

我知道今天是:

[NSDate date]

然后我该怎么做?

7 个答案:

答案 0 :(得分:5)

使用NSDateComponents,如下所示:

NSCalendar *gregorian = [[NSCalendar alloc]
    initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *today = [NSDate date];

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

    NSUInteger todaysWeek = [todaysComponents week];


    NSDate *anotherDate = [NSDate date];

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

    NSUInteger anotherWeek = [otherComponents week];

    if(todaysWeek==anotherWeek){
         NSLog(@"another date is this week");
    }else if(todaysWeek+1==anotherWeek){
         NSLog(@"another date is next week")
    }

您还可以使用月份或年份等其他组件来完全确定。

注意:不要使用timeIntervals。通过使用NSDateComponents,您可以忽略小时,分钟和秒。我想你想要那个。

答案 1 :(得分:2)

将2个日期传递给此方法:

- (BOOL) isSameWeekAsDate: (NSDate *) aDate andDate:(NSDate *) bDate
{
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:aDate];
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:bDate];

    if ([components1 week] != [components2 week]) return NO;

    //return (abs([self timeIntervalSinceDate:aDate]) < 604800); // ops, forgot to change "self" with parameter "bDate":
    return (abs([bDate timeIntervalSinceDate:aDate]) < 604800);
}

修改 称之为不同年份的2个日期:

[components setDay:31];
[components setMonth:12];
[components setYear:2010];
NSCalendar *gregorian = [[NSCalendar alloc]  initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date1 = [gregorian dateFromComponents:components];

// second date (SATURDAY -of the same week, other year...)
[components setDay:1];
[components setMonth:1];
[components setYear:2011];
NSDate *date2 = [gregorian dateFromComponents:components];

if ([self isSameWeekAsDate:date1 andDate:date2]) {
    NSLog(@"Same Week!");
}else{
    NSLog(@"OTHER WEEK!");
}

答案 2 :(得分:1)

我正在添加我的解决方案(作为 NSDate类别实施),它不使用周,在iOS7,iOS8中已弃用

- (NSDate *)firstDateOfWeek {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *startOfWeek;
    [calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&startOfWeek interval:NULL forDate:self];
    return startOfWeek;
}

- (BOOL)isSameWeekWithDate:(NSDate *)date {
    if (ABS(self.timeIntervalSince1970 - date.timeIntervalSince1970) > (7 * 24 * 60 * 60)) {
        return NO;
    }    
    return ([[self firstDateOfWeek] timeIntervalSince1970] == [[date firstDateOfWeek] timeIntervalSince1970]);
}

- (BOOL)isThisWeek {
    return [self isSameWeekWithDate:[NSDate new]];
}

答案 3 :(得分:0)

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:yourDate];
NSInteger week = [components week]; // here your have a Integer with the weeknr of yourDate

components = [cal components:NSWeekCalendarUnit fromDate:[NSDate date]];
weekToday = [components week]; // here your have a Integer with the weeknr of today

你可以做的其余部分。 在一年的最后一周,它可能会得到另一个脑力劳动。

清除?

答案 4 :(得分:0)

注意:这会使用周日周六的周概念来计算“周”的概念。

要计算一周中的当前日期,请使用here中的以下内容:

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

NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dateOfInterest];

NSInteger weekday = [weekdayComponents weekday];
// weekday 1 = Sunday for Gregorian calendar

[gregorian release];

剩下的应该是微不足道的。从NSDate获取当前日期和日期。使用日期和日期查找开始日期和结束日期(如果今天是星期一,那么date - 1 day是一周的第一天等)。弄清楚日期在哪一周。

答案 5 :(得分:0)

对于iOS 7及更高版本,您必须将NSWeekCalendarUnit替换为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;
    }

}

答案 6 :(得分:0)

忘记日期组件,你必须调整很多东西(检查它是否是一年中的最后一周,检查周日是否是一周的开始......)并且容易出错和意大利面条代码。

这解决了您的问题,并扩展为检测&#34;上周&#34;,&#34;前几周&#34;,&#34;本周&#34;,&#34;下周&#34 ;和&#34;以后几周&#34;

-(EventWeekRange)numberOfWeeksFromTodayToEvent:(NSDate *)eventDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSComparisonResult comparison = [calendar compareDate:[NSDate date] toDate:eventDate toUnitGranularity:NSCalendarUnitWeekOfYear];
    if (comparison == NSOrderedSame) {
        return RangeThisWeek;
    } else if (comparison == NSOrderedAscending) { // The event date is in the future
        // Advance today's date one week to check if this new date is in the same week as the event
        NSDate *todaysNextWeek = [[NSDate date]dateByAddingTimeInterval:60*60*24*7];
        if ([calendar compareDate:todaysNextWeek toDate:eventDate toUnitGranularity:NSCalendarUnitWeekOfYear] == NSOrderedSame) {
            return RangeNextWeek;
        } else {
            return RangeLater;
        }
    } else { // The event date is in the past
        // Advance the event's date one week to check if this new date is in the same week as today
        NSDate *eventsNextWeek = [eventDate dateByAddingTimeInterval:60*60*24*7];
        if ([calendar compareDate:eventsNextWeek toDate:[NSDate date] toUnitGranularity:NSCalendarUnitWeekOfYear] == NSOrderedSame) {
            return RangeLastWeek;
        } else {
            return RangeEarlier;
        }
    }
}