Compare NSDate for Today or Yesterday

前端 未结 4 1316
-上瘾入骨i
-上瘾入骨i 2020-12-30 10:03

Well I guess this has been asked a thousand times, but for some reason the answeres dont really work or had other problems,....

Anyway here is what I have \"working\

4条回答
  •  青春惊慌失措
    2020-12-30 10:44

    I know this is an old question, but I was just doing something similar myself and came upon it.

    First of all, since iOS 4.0 (and Mac OS 10.6), NSDateFormatter can do relative dates, which gives you "Today" and "Yesterday" automatically.

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setDoesRelativeDateFormatting:YES];
    
    NSString* dateString = [formatter stringFromDate:[NSDate date]];    
    NSLog( @"date = %@", dateString );
    

    Which outputs:

    2014-03-15 15:26:37.683 TestApp[1293:303] date = Today
    

    However, the OP was asking how to compare an NSDate for today or yesterday, something I also wanted to do, relative date formatting aside. Here's what I came up with, implemented as a category on NSDate:

    @implementation NSDate (IsToday)
    
    - (BOOL) isToday
    {
        NSCalendar* calendar = [NSCalendar currentCalendar];
    
        // Components representing the day of our date.
        NSDateComponents* dateComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                 fromDate:self];
        NSDate* date = [calendar dateFromComponents:dateComp];
    
        // Components representing today.
        NSDateComponents* todayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                  fromDate:[NSDate date]];
        NSDate* todayDate = [calendar dateFromComponents:todayComp];
    
        // If the dates are equal, then our date is today.
        return [date isEqualToDate:todayDate];
    }
    
    @end
    

    The reason I wanted to do this is so that I could display the time of things created today, and the date of things not created today - similar to how Mail.app shows the dates on messages. It looks like this:

    - (NSString*) postDateToString:(NSDate*)aDate
    {
        static NSDateFormatter* todayFormatter = nil;
        if( todayFormatter == nil ) {
            todayFormatter = [[NSDateFormatter alloc] init];
            [todayFormatter setTimeStyle:NSDateFormatterShortStyle];
            [todayFormatter setDateStyle:NSDateFormatterNoStyle];
        }
    
        static NSDateFormatter* notTodayFormatter = nil;
        if( notTodayFormatter == nil ) {
            notTodayFormatter = [[NSDateFormatter alloc] init];
            [notTodayFormatter setTimeStyle:NSDateFormatterNoStyle];
            [notTodayFormatter setDateStyle:NSDateFormatterShortStyle];
            [notTodayFormatter setDoesRelativeDateFormatting:YES];
        }
    
        NSDateFormatter* formatter = notTodayFormatter;
    
        if( [aDate isToday] ) {
            formatter = todayFormatter;
        }
    
        return [formatter stringFromDate:aDate];
    }
    

提交回复
热议问题