Check if there is NSDate this week or next week

Is there a way to check if NSDate is this week or next week?

I know that today there are:

[NSDate date]

and then how can I do this?

+3
source share
7 answers

Use NSDateComponents, something like this:

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")
    }

You can also fully use other components, such as a month or a year.

NOTE: Do not use timeIntervals. Using NSDateComponents, you ignore hours, minutes, and seconds. I think you want it.

+5
source

pass 2 dates to this method:

- (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);
}

EDIT: call it two dates of different years:

[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
source

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

. , .

?

0

: "", -.

:

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 - ..). , .

0

For iOS 7 and above, you need to replace NSWeekCalendarUnitwithNSCalendarUnitWeekOfYear

- (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
source

Forget about date components, you need to set up a lot of things (check if it passed last week of the year, check if Sunday is the beginning of the week ...) and is prone to spaghetti errors and codes.

This solves your problem and extends to the detection of "last week", "previous weeks", "this week", "next week" and "later weeks"

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

All Articles