Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter relative date formatting with custom format

So my intention is to put out dates that would look like the following:

Today, August 28
Tomorrow, August 29
Friday, August 30
...etc

The issue is that it seems I can only get so close.

When I setDoesRelativeDateFormatting:YES and setDateStyle to Full and setTimeStyle to None, it yields results like this:

Today
Tomorrow
Friday, August 30, 2013

This yields the year and does not yield month and date for today and tomorrow.

The other code I've tried is this:

[NSDateFormatter dateFormatFromTemplate:@"eeeedMMM" options:0
                                                     locale:[NSLocale currentLocale]];

and that yields the following:

Wednesday, August 28
Thursday, August 29
Friday, August 30

Seemingly, the dateStyle and timeStyle, or the relative date formatting overrides the custom format.

Is there a way to accomplish this date format without going into a custom implementation?

Thanks in advance!

like image 391
user1869469 Avatar asked Aug 28 '13 21:08

user1869469


1 Answers

Based on the documentation about NSDateFormatter I think that the only solution for your issue is to present two date strings for the days you would like to be relatively shown. You should keep in mind that in different languages there are differences in the relative dates. From the documentation:

If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date. The available phrases depend on the locale for the date formatter; whereas, for dates in the future, English may only allow “tomorrow,” French may allow “the day after the day after tomorrow".

I believe that if you define that you're going to show relatively for example yesterday, today and tomorrow you can use 2 NSDateFormatters. With the first one you will show the relative value and with the second you will display the actual date. For the non-relative dates you will display only the non-relative value.

like image 130
Lubakis Avatar answered Oct 13 '22 00:10

Lubakis