2014-04-13 111 views
3

我想要檢測的是特定日期是在過去7天內(上週內)。 我發現了一個方法來做到這一點,但它在桌面視圖(聊天控制器)中吃我的表現。iOS檢測日期是否在上週

該代碼張貼在下面,我知道它可以做得更好,但我沒有想法。有人有更好的方法嗎?謝謝!

代碼:

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"; 
} 
} 
+0

與上週你是指現在的最後7天,或最後一週星期一到星期日或星期天到星期六? – vikingosegundo

+0

現在7天前(或者我會比較weeknumber,但這不起作用,因爲兩個日期可以在7天內,但在2個不同的星期) –

+0

好吧,據我瞭解,我回答了。今天是否包括在過去7天內?我沒有假設。 – vikingosegundo

回答

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; 
} 
+0

今天不包括在內。我試圖檢查1)今天2)昨天3)直到7天前的剩餘天數4)7天前。我會盡量玩你的答案。你也歡迎給它一個鏡頭:) ...謝謝! –

+0

您將可以調整此代碼或每個案例。 – vikingosegundo

+0

恰好我會改變每個比較的比較。將發佈編輯中的最終代碼... 5分鐘:) –