iosdatefutureuser-friendly

ios/xcode/objective-c: Display future dates in friendly format


I have found some great solutions for displaying past dates in user friendly format such as just now, a few hours ago, yesterday, last week etc.

However, I've been unable to find a similar friendly format for future events/appointments.

I'm looking for something like, Today at 2PM. Tomorrow, Wednesday at 3:45PM. next Tuesday, September, 12th at 2PM. or for further away dates Friday, November 12th, 2015 at 2PM.

Can anyone recommend a category that does this, a tutorial or provide guidance in the form of a method to tackle this?

Thanks in advance for any suggestions.


Solution

  • Best user friendly date :

    The solution is the same. You can build your solution on the link you mentioned.

    [[NSDate date] timeIntervalSinceDate:date]; is a simple subtraction between two date who return positive or negative double if date is in the past or the futur.

    After verifying if the date is in the past or the future some mathematics will give you the good user friendly string. For example, you can change the format depending on the parameters.

    Simple user friendly :

    If what you want is always string like "Wednesday, November 15th at 02:00PM", you can use NSDateFormater.

    For your case :

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE, MMMM dd 'at' HH:mma"];
    
    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
    
    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    

    With this method date string will always take the same format.

    Hope that's will help.