简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM