4

I am trying to detect is a specific date is within the last 7 days (within the last week). I have found a method to do that, but its eating my performance in a tableview (chat controller).

The code is posted below, and i know it can be done a LOT better, but i am running out of ideas. Does somebody have better approach? thanks!

The code:

NSDate *date = [[_messages objectAtIndex:indexPath.row] date];
NSString *lastMessageDateString = [[[NVDate alloc] initUsingDate:date] stringValueWithFormat:@"dd/MM/yy"];
NSString *todayDateString = [[[NVDate alloc] initUsingToday] stringValueWithFormat:@"dd/MM/yy"];
NSString *yesterdayDateString = [[[[NVDate alloc] initUsingToday] previousDay] stringValueWithFormat:@"dd/MM/yy"];
NSString *earlier2DaysDateString = [[[[NVDate alloc] initUsingToday] previousDays:2] stringValueWithFormat:@"dd/MM/yy"];
NSString *earlier3DaysDateString = [[[[NVDate alloc] initUsingToday] previousDays:3] stringValueWithFormat:@"dd/MM/yy"];
NSString *earlier4DaysDateString = [[[[NVDate alloc] initUsingToday] previousDays:4] stringValueWithFormat:@"dd/MM/yy"];
NSString *earlier5DaysDateString = [[[[NVDate alloc] initUsingToday] previousDays:5] stringValueWithFormat:@"dd/MM/yy"];
NSString *earlier6DaysDateString = [[[[NVDate alloc] initUsingToday] previousDays:6] stringValueWithFormat:@"dd/MM/yy"];
NSString *earlier7DaysDateString = [[[[NVDate alloc] initUsingToday] previousDays:7] stringValueWithFormat:@"dd/MM/yy"];
//return the time of the message since it is today
if ([lastMessageDateString isEqualToString:todayDateString]) {
    return [SAD.myE.timeFormat stringFromDate:date];
}
//return the string "yesterday" of the message since it is yesterday
else if ([lastMessageDateString isEqualToString:yesterdayDateString]) {
    return @"Yesterday";
}
//return the string of the actual day of the message since it is within last 7 days
else if ([lastMessageDateString isEqualToString:earlier2DaysDateString] ||
         [lastMessageDateString isEqualToString:earlier3DaysDateString] ||
         [lastMessageDateString isEqualToString:earlier4DaysDateString] ||
         [lastMessageDateString isEqualToString:earlier5DaysDateString] ||
         [lastMessageDateString isEqualToString:earlier6DaysDateString] ||
         [lastMessageDateString isEqualToString:earlier7DaysDateString]) {
    return [SAD.myE.dayFormat stringFromDate:date];
}
//return the string date since the message is olf
else {
    return [SAD.myE.dateFormat stringFromDate:date];
}

EDIT Thanks to @vikingosegundo i have tried to use his solution to implement this solution. He since then made a category that made it even easier that i will try and implement now. Just for the curious ones:

- (NSString *)stringForDate:(NSDate *)date {
NSDate *now = [NSDate date];  // now
NSDate *today;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit // beginning of this day
                                startDate:&today // save it here
                                 interval:NULL
                                  forDate:now];

NSDateComponents *comp = [[NSDateComponents alloc] init];
comp.day = 0;
NSDate * theDayToday = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:today options:0];
if ([date compare:theDayToday] == NSOrderedDescending) {
    return @"today";
}

comp.day = -1;
NSDate * yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:today options:0];
if ([date compare:yesterday] == NSOrderedDescending) {
    return @"yesterday";
}

comp.day = -7;      // lets go 7 days back from today
NSDate * oneWeekBefore = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:today options:0];
if ([date compare:oneWeekBefore] == NSOrderedDescending) {
    return @"within 7 days";
} else {
return @"before 7 days";
}
}
B-Man
  • 2,209
  • 3
  • 22
  • 35
  • with last week do u mean the last 7 days before now, or the last week as monday to sunday or sunday to saturday? – vikingosegundo Apr 13 '14 at 23:39
  • 7 days before now (or i would compare weeknumber but that does not work here since two dates can be within 7 days but on 2 different weeks) – B-Man Apr 13 '14 at 23:42
  • ok, as far as I understood, I answered it. Is today included in the last 7 days? I assumed not. – vikingosegundo Apr 13 '14 at 23:57
  • @vikingosegundo today is not included. i am trying to check for 1) today 2) yesterday 3) remaining days till 7 days ago 4) before 7 days ago. I will try and play with your answer. you are also welcome to give it a shot :) ... thanks! – B-Man Apr 13 '14 at 23:59
  • if you are not keen on implementing this yourself, try https://github.com/MatthewYork/DateTools – Chris Chen Apr 13 '14 at 23:31

1 Answers1

5
-(BOOL) dayOccuredDuringLast7Days
{

    NSDate *now = [NSDate date];  // now
    NSDate *today;
    [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit // beginning of this day
                                    startDate:&today // save it here
                                     interval:NULL
                                      forDate:now];

    NSDateComponents *comp = [[NSDateComponents alloc] init];
    comp.day = -7;      // lets go 7 days back from today
    NSDate * oneWeekBefore = [[NSCalendar currentCalendar] dateByAddingComponents:comp 
                                                                           toDate:today 
                                                                          options:0];


    if ([self compare: oneWeekBefore] == NSOrderedDescending) {

        if ( [self compare:today] == NSOrderedAscending ) { // or now?
            return YES;
        }
    }
    return NO;
}

a complete command line example for last 7 days and yesterday. as category on NSDate

#import <Foundation/Foundation.h>



@interface NSDate (ExtendedComparions)
-(BOOL) dayOccuredDuringLast7Days;
-(BOOL) dayWasYesterday;
@end

@implementation NSDate (ExtendedComparions)


-(BOOL) _occuredDaysBeforeToday:(NSUInteger) nDaysBefore
{
    NSDate *now = [NSDate date];  // now
    NSDate *today;
    [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit // beginning of this day
                                    startDate:&today // save it here
                                     interval:NULL
                                      forDate:now];

    NSDateComponents *comp = [[NSDateComponents alloc] init];
    comp.day = -nDaysBefore;      // lets go N days back from today
    NSDate * before = [[NSCalendar currentCalendar] dateByAddingComponents:comp 
                                                                    toDate:today 
                                                                   options:0];
    if ([self compare: before] == NSOrderedDescending) {
        if ( [self compare:today] == NSOrderedAscending ) {
            return YES;
        }
    }
    return NO;
}


-(BOOL) dayOccuredDuringLast7Days
{
    return [self _occuredDaysBeforeToday:7];
}

-(BOOL) dayWasYesterday
{
    return [self _occuredDaysBeforeToday:1];
}


@end



int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSDate *now =[NSDate date];
        NSDate *twoDaysBefore = [[NSCalendar currentCalendar] dateByAddingComponents:(
                                                                                      {
                                                                                          NSDateComponents *comps = [[NSDateComponents alloc] init];
                                                                                          comps.day = -2;
                                                                                          comps;
                                                                                      })
                                                                              toDate:now
                                                                             options:0];

        if ([twoDaysBefore dayOccuredDuringLast7Days]) {
            NSLog(@"last week");
        } else {
            NSLog(@"not last week");
        }

        if ([twoDaysBefore dayWasYesterday]) {
            NSLog(@"yesteday");
        } else {
            NSLog(@"not yesterday");
        }


    }
    return 0;
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • today is not included. i am trying to check for 1) today 2) yesterday 3) remaining days till 7 days ago 4) before 7 days ago. I will try and play with your answer. you are also welcome to give it a shot :) ... thanks! – B-Man Apr 14 '14 at 00:00
  • exactly i will change the comp for each comparison. will post the final code that works in an edit... 5 minutes :) – B-Man Apr 14 '14 at 00:01
  • I posted for a second case, yesterday. but I also show how to re-use the code. – vikingosegundo Apr 14 '14 at 00:15
  • That is faboulous... i will try to implement it myself before i look into your code (gotta do it the hard way BUT i will look at it in asap and get back to you... thank you) – B-Man Apr 14 '14 at 00:18
  • Does this take into account the actual time/clock of the date when you add the comp? – B-Man Apr 14 '14 at 00:20
  • 1
    it adds the comps to today, and that is the beginning of the day in the timezone of the calendar. – vikingosegundo Apr 14 '14 at 00:22
  • Perfect... i implemented the first solution (see edit) and it worked.. THANKS! will try and add your category instead now and save it for later use also... thanks again! – B-Man Apr 14 '14 at 00:27
  • Accepted as answer and +1!. Thanks – B-Man Apr 14 '14 at 00:31