iOS检测日期是否在上周

时间:2014-04-13 23:28:16

标签: ios nsdate date-comparison date-manipulation

我正在尝试检测特定日期是否在过去7天内(在上周内)。 我找到了一种方法,但它在桌面视图(聊天控制器)中吃掉了我的表现。

代码发布在下面,我知道它可以做得更好,但我的想法已经用完了。有人有更好的方法吗?谢谢!

代码:

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];
}

修改 感谢@vikingosegundo,我试图使用他的解决方案来实现这个解决方案。从那以后,他创建了一个类别,使我现在可以更轻松地尝试和实施。只是为了好奇的人:

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

1 个答案:

答案 0 :(得分: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;
}

过去7天和昨天的完整命令行示例。作为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;
}