0

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.

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
Jeff
  • 1,405
  • 3
  • 26
  • 46
  • 1
    Have you tried to implement something? Sound pretty easy. Compare Date with Today. If it's today use Today, if it's less than one week ago use weekday, otherwise use exact date. – Matthias Bauch Jul 16 '13 at 09:10
  • @MatthiasBauch Thaks for your reply.I haven't tried it out.As I am new in iPhone development – Jeff Jul 16 '13 at 09:12
  • 1
    Google before posting Question. You should have found answer. – NightFury Jul 16 '13 at 09:23

3 Answers3

1

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.
SFeng
  • 2,226
  • 1
  • 19
  • 16
1

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.

John
  • 2,640
  • 1
  • 16
  • 16
0

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

Zen
  • 3,047
  • 1
  • 20
  • 18
  • 1
    Since `cccc` is used for the stand-alone week day I would recommend to use `EEEE` instead. The stand-alone versions should really only be used if they are used alone (e.g. in a calendar header). In russian, finnish or italian locale there is a difference between those two. – Matthias Bauch Jul 16 '13 at 10:14