简体   繁体   English

ios / xcode / objective-c:以友好格式显示将来的日期

[英]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. 我正在寻找类似今天下午2点的内容。 Tomorrow, Wednesday at 3:45PM. 明天,星期三下午3:45。 next Tuesday, September, 12th at 2PM. 9月12日(星期二)下午2点。 or for further away dates Friday, November 12th, 2015 at 2PM. 或其他日期,即2015年11月12日(星期五)下午2点。

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 . 如果您想要的字符串始终是“ 11月15日,星期三,02: 00PM ”,则可以使用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. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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