Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On setting doesRelativeDateFormatting = YES, stringFromDate returns nil

In the NSDateFormatter 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.

I am using the following code:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE dMMMM',' hma"
                                                       options:0
                                                        locale:[NSLocale currentLocale]];
formatter.doesRelativeDateFormatting = YES;
NSLog(@"Date: %@", [formatter stringFromDate:[NSDate date]]);

It logs Date:.

But on commenting out formatter.doesRelativeDateFormatting = YES; the output comes out correct.

Date: Tuesday, 19 August 1:18 pm

Am I understanding doesRelativeDateFormatting wrong?

like image 737
Ayush Goel Avatar asked Aug 19 '14 07:08

Ayush Goel


1 Answers

Hi when i modified code like this. it works..

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE dMMMM',' hma"
                                                       options:0
                                                        locale:[NSLocale currentLocale]];
 [formatter setDateStyle:NSDateFormatterFullStyle];
 [formatter setTimeStyle:NSDateFormatterShortStyle];
 formatter.doesRelativeDateFormatting = YES;
  NSLog(@"Date: %@", [formatter stringFromDate:[NSDate date]]);
like image 110
Sreejith Bhatt Avatar answered Sep 30 '22 15:09

Sreejith Bhatt