25

apple use it in the messages app for today messages Today 11:45 AM Yesterday 11:45 AM

i see it in the apple developer site

To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35. The version of the standard varies with release of the operating system:

Calendar Fields

fields ( alias | (field*, special*)) > field ( alias | (displayName?, relative*, special*)) >

Translations may be supplied for names of calendar fields (elements of a calendar, such as Day, Month, Year, Hour, and so on), and for relative values for those fields (for example, the day with relative value -1 is "Yesterday"). Where there is not a convenient, customary word or phrase in a particular language for a relative value, it should be omitted.

how to use it in Xcode?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user301212
  • 311
  • 1
  • 4
  • 11

1 Answers1

48

use – setDoesRelativeDateFormatting:

NSDateFormatter *dateFormatter = [NSDateFormatter new];

dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;

dateFormatter.doesRelativeDateFormatting = YES;

NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

this logs Today, 11:30 AM on en_US locales.

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterFullStyle]; [formatter setDoesRelativeDateFormatting:YES]; NSDate * date = [NSDate date]; NSLog(@"%@", [formatter stringFromDate:date]); – user301212 Sep 08 '13 at 10:16