1

My Goal: If the current date is "Today" or "Yesterday", use the words "Today" and "Yesterday". If the current date is before today or yesterday, then use the following format (Thu, Aug 15). I am able to achieve this format using @"EEE, MMM d" when I set setDoesRelativeDateFormatting to NO, however when I attempt to use the following code it breaks down and displays Aug, 12 2013, which is not what I want...

// Set up the date formatter
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        dateFormatter.dateFormat = @"EEE, MMM d";


        [dateFormatter setDoesRelativeDateFormatting:YES];
        [dateFormatter setLocale:[NSLocale currentLocale]];
Apollo
  • 8,874
  • 32
  • 104
  • 192

1 Answers1

0

This is a hack as I could not find a built in way to go about performing this task but it works almost flawlessly. The only problem is that it outputs the full name of the month rather than a short output. This of course could be fixed in a way similar to what I did with the year in the following code if need be.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];

    NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];

    NSString *theYear = [NSString stringWithFormat:@", %ld", comps.year];

    [dateFormatter setDoesRelativeDateFormatting:YES];

    [dateFormatter setLocale:[NSLocale currentLocale]];

    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    if ([dateString isEqualToString:@"Today"] || [dateString isEqualToString:@"Yesterday"]) {
        NSLog(@"%@", dateString);
    } else {
        dateString = [dateString stringByReplacingOccurrencesOfString:theYear withString:@""];
        NSLog(@"%@", dateString);
    }

When relative formatting is on it prints Today, when its off it prints Friday, August 16

Peter Foti
  • 5,526
  • 6
  • 34
  • 47
  • A little warning: this solution does not handle localised devices well. – Rob van der Veer Aug 16 '13 at 06:57
  • That is correct, if you change the locale it will break. You can change `theYear` string without the comma and that will allow you to remove the year in any locale. Unfortunately viernes, 16 de agosto de is not a proper way to output the date in Spanish. – Peter Foti Aug 16 '13 at 07:04
  • The documentation on `relativeDateFormatting` is quite clear on the subject: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setDoesRelativeDateFormatting: – Rob van der Veer Aug 16 '13 at 07:33
  • Im not sure what you're getting at @RobvanderVeer If the relativeDateFormat is set to YES, then it works in all locales. If, its not the format will show up as what I copied above for Spanish `16 de agosto de` – Peter Foti Aug 16 '13 at 07:36
  • Sorry, maybe i didn't understand the problem well enough. – Rob van der Veer Aug 16 '13 at 07:40