Question

I want to display a date in the following way:

  • If it is today, then display the time only.
  • If it is in the past week, then display "Yesterday", "Sunday", "Monday" etc.
  • If it is more than one week ago, then display the actual date.

I am trying something to get something similar to how the date is displayed in the iPhone SMS application.

Was it helpful?

Solution

why no use the method of

setDoesRelativeDateFormatting:

with it set to YES. Now the formatter now will return 'today' or 'yesterday' or 'tomorrow' depending on the date that should be formatted. If a relative format can't be found, then use the formatter will be used for the format.

OTHER TIPS

For more format patterns as below:

  • Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.
  • Formatters in iOS 5.0-5.1 use version tr35-19.
  • Formatters in OS X v10.7 and iOS 4.3 use version tr35-17.
  • Formatters in iOS 4.0-4.2 use version tr35-15.
  • Formatters in iOS 3.2 use version tr35-12.
  • Formatters in OS X v10.6 and iOS 3.0-3.1 use version tr35-10.

Go through NSDateFormatter(link here) and NSDate (link here) class documentation before asking a thing you have not even tried.

The NSDateFormatter class can be used to properly format the date into your choice with fixed parameters for different components of the date. For showing the full name of the day, use 'cccc' in the format. For eg

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormat:@"cccc dd-MM, HH:mm:ss"];

To compare dates, there are in built methods for comparing NSDate objects for eg isEqualToDate , laterDate: , timeIntervalSinceNow etc. Go through the Apple documentation link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top