1

I use [formatter setDateFormat:@"EEEE, MMMM dd, h:mma"];

So EEEE - show for me a name of the day. But I want to display Today or Tomorrow or Yesterday instead and in another cases display name of the day.

So I want to use format like I've described above an example:

Monday, OCT 2, 4:33PM

but if the Monday is today I want to show:

Today, OCT 2, 4:33PM

If Monday was yesterday it has to show:

Yesterday, OCT 2, 4:33PM

If use this code you suggested as duplicated:

formatter.dateStyle = NSDateFormatterShortStyle;
formatter.timeStyle = NSDateFormatterShortStyle;
formatter.doesRelativeDateFormatting = YES;

10/2/15, 4:33PM which is not so I want to expect.

In example you point as duplicated: how to use NSDateFormatter to see "Today" string there is 2 answer second one it's just display name of the day but not "TODAY" for example, so first answer there I've described above why it does not work, so it's not so right suggestion to say that my question duplicate this link. Maybe it can duplicate another link, but not this one.

Of course I can parse manually received string from formatter to find a day and replace it with "TODAY" if needed, but I just asked if formatter can make it automatically?

Community
  • 1
  • 1
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

1 Answers1

0

Creating an NSDateFormatter that only contains the weekday will do what you want.

NSDate *now = [NSDate date];
NSDateFormatter *weekday = [[[NSDateFormatter alloc] init]    autorelease];
[weekday setDateFormat: @"EEEE"];
NSLog(@"The day of the week is: %@", [weekday stringFromDate:now]);

if you need internationalization, the NSDateFormatter can be sent a locale, which will give the proper translation.

if it doesn't help tell me the result to test another things

Edit

-(NSString *) calculateDay {

int differenceInDays;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-DD"];

int differenceInSeconds = fabs([self timeIntervalSinceNow]);

NSCalendar *currentCalendar = [NSCalendar currentCalendar];

// Getting components of the current date.
NSDateComponents *currentDateComponents = [currentCalendar components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSTimeZoneCalendarUnit  fromDate:[NSDate date]];
// Getting components of the |date|.
NSDateComponents *dateComponents = [currentCalendar components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSTimeZoneCalendarUnit  fromDate:self];
// Getting components of the difference between now and |date|.

NSDateComponents *differenceComponents = [currentCalendar components:NSDayCalendarUnit fromDate:self toDate:[NSDate date] options:0];
// NSDateComponents along with Calendar handles the timezone difference.
[currentDateComponents setTimeZone:[NSTimeZone systemTimeZone]];
[dateComponents setTimeZone:[NSTimeZone systemTimeZone]];
[differenceComponents setTimeZone:[NSTimeZone systemTimeZone]];

differenceInDays = (int)[differenceComponents day];
// This very bad if is because the components:fromDate:toDate:options: calcuates the difference in days with 24hrs difference
// So I had to handle the difference in days myself. -Mepla-
if (differenceInSeconds > [differenceComponents day]*24*3600 + [currentDateComponents hour]*3600 + [currentDateComponents minute]*60 + [currentDateComponents second]){
    differenceInDays++;
}
switch (differenceInDays) {
    case 0:
        return @"Today";
        break;

    case 1:
        return [BLLocalize getTextWithKey:@"Yesterday"];
        break;

    case 2:
    case 3:
    case 4: return [NSString stringWithFormat:@"%@", [[dateFormatter shortWeekdaySymbols] objectAtIndex:[dateComponents weekday]-1]]; // -1 is because the array starts at 0. -Mepla-

        break;

    default:{
        NSString *year = [NSString stringWithFormat:@"%ld", (long)[dateComponents year]];
        return [NSString stringWithFormat:@"%ld/%ld/%@", (long)[dateComponents month], (long)[dateComponents day], [year substringFromIndex: [year  length] - 2]];
    }
        break;
}

}

engmahsa
  • 314
  • 1
  • 7
  • thanks for answer but EEEE just show a name of the day but not "TODAY" "YESTERDAY" or "TOMORROW" – Matrosov Oleksandr Oct 06 '15 at 14:05
  • I've update my answer plz check the edition this method will help u – engmahsa Oct 06 '15 at 14:10
  • oh, thanks) actually I have NSDate category that calculate for me and return if my date is Today or Tomorrow or Yesterday, it's very simple for me just append few strings. I just asked maybe someone know if formatter can do it automatically when we use like EEEE format to get name of the week day. So formatter.doesRelativeDateFormatting it's like a solution but it works only with formatter.dateStyle = NSDateFormatterShortStyle; and display "Today" if needed, but if I use custom format [formatter setDateFormat:@"EEEE, MMMM dd, h:mma"]; it won't work. – Matrosov Oleksandr Oct 06 '15 at 14:48