如何在 Objective-C 中格式化日期,类似于 jquery.timeago 库?
我有一个在表格单元格中显示的项目提要,其中一部分是过去的日期/时间戳。
在 Objective-C 中,如何以与网络上的 jquery.timeago 插件 相同的方式完成它们的格式化?
也就是说,接受一个日期并输出如下内容:
- “刚刚”
- “2 分钟前”
- “24 天前”
- “一个月前”
我看到有一个 这里是 NSDate 扩展类,其中包含 dateWithDaysBeforeNow、dateWithMinutesBeforeNow 等方法,等等,但如果有一个图书馆已经做到了这一点,我会使用它。
编辑:此外,如果有人编写一个方法(获取日期,返回一个字符串)来完成此操作,无论是使用此链接的扩展库还是其他方法,我都会奖励他们答案。
编辑 2 赏金奖励给任何能够在 Objective-C 中编写模糊日期算法的人。
I have a feed of items displayed in table cells, part of which is a date / timestamp in the past.
In Objective-C, how can I accomplish formatting them in the same manner as the jquery.timeago plugin on the web?
That is, taking in a date and outputting things like:
- 'just now'
- '2 minutes ago'
- '24 days ago'
- 'a month ago'
I see there is an NSDate extension class here with methods such as dateWithDaysBeforeNow, dateWithMinutesBeforeNow, etc, but if there is a library out there that has already done this I will use it.
EDIT: Further to this, if someone composes a method (takes a date, returns a string) that accomplishes this, either with this linked extensions library or another method, I will award them the answer.
EDIT 2 Bounty goes to whoever can write the fuzzy date algorithm in Objective-C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
创建静态类方法:
Create Static Class Method:
好吧,我昨晚睡不着——我想我会咬人的。这是 jQuery timeAgo 的逐行移植,注释中附有原始 JavaScript 源代码以供参考。使用标准 ObjC/AppKit 机制进行用户默认设置和本地化。与 Web 相关的内容,例如更新现有 DOM 元素,显然被省略了。还省略了可以在设置中放置函数的机制,因为这是 JavaScript 特定的。话不多说:
头文件:
和实现文件:
OK, I couldn't sleep last night -- guess I'll bite. Here's a line-for-line port of jQuery's timeAgo, with the original JavaScript source in comments for reference. Uses standard ObjC/AppKit mechanisms for user defaults and localization. Web-related stuff, like updating an existing DOM element is obviously omitted. Also omits the mechanism where you can put a function in the settings, because that's sort of JavaScript specific. Without further ado:
Header file:
And the implementation file:
我编写了一个 NSDate 类别,它以人类可读的格式返回时间增量(例如,“4 周,2 天”)。您可以在此处下载并根据您的需要进行调整:https://github.com/chriscdn/RHTools (寻找 NSDate+timesince)。
I wrote an NSDate category that returns time deltas in a human readable format (e.g., "4 weeks, 2 days"). You can download it here and tweak it to your needs: https://github.com/chriscdn/RHTools (look for NSDate+timesince).
我知道这已经很旧了,但这就是我所做的,相当简单......
I know this is old but this is what I did which is fairly simple...
您可以尝试使用 NSDateFormater 并自行编写。
如果值小于一分钟,则立即写入,否则为分钟、小时、天等。
You can try with NSDateFormater and write it your self.
If value is under one minute then write just now, otherwise minutes, hours, days, etc.
https://stackoverflow.com/a/8268903/1915957
↑ 转换 swift 代码。
https://stackoverflow.com/a/8268903/1915957
↑ convert swift code.
如果您对日期和时间有疑问。
你应该想到这个库
https://github.com/MatthewYork/DateTools
它总是有帮助!
if you have a problem with date and time.
you should think to this library
https://github.com/MatthewYork/DateTools
It always help!
您可以使用NSDate-TimeAgo。可以使用 CocoaPods 将其安装在您的项目中。
#import "NSDate+TimeAgo.h"
导入标头You can use NSDate-TimeAgo. It can be installed in your project using CocoaPods.
#import "NSDate+TimeAgo.h"