Jeff
Jeff

Reputation: 1403

How to format in NSDate depending on the time difference?

I want to display a date in the following way:

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

Upvotes: 0

Views: 118

Answers (3)

John
John

Reputation: 2660

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.

Upvotes: 1

SFeng
SFeng

Reputation: 2276

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.

Upvotes: 1

Zen
Zen

Reputation: 3117

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

Upvotes: 0

Related Questions