设置相对日期的格式
是否有一个 ruby gem 可以格式化相对于当前时间的日期?我想要像“明天下午 5 点”、“下周星期四下午 5:15”这样的输出,我不太关心确切的输出,只要它是自然语言中的相对日期即可
Is there a ruby gem that will format dates relative to the current time? I want output like "Tomorrow at 5pm", "Thursday next week at 5:15pm", I'm not too concerned about the exact output, just as long as it's relative dates in natural language
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你有rails,我认为 ActionView::Helpers:: DateHelper#distance_of_time_in_words 对此有所帮助。
如果相对于当前时间,请使用
distance_of_time_in_words_to_now
而不是distance_of_time_in_words
。如果您的应用是基于 Rails 的,则只需在视图中使用
distance_of_time_in_words
、distance_of_time_in_words_to_now
、time_ago_in_words
即可。if you have rails, I think ActionView::Helpers::DateHelper#distance_of_time_in_words helps that.
In case of relative to the current time, use
distance_of_time_in_words_to_now
instead ofdistance_of_time_in_words
.If your app is rails-based, just use
distance_of_time_in_words
,distance_of_time_in_words_to_now
,time_ago_in_words
in view.尝试一下“慢性”宝石,
它完全符合您的要求。
try the gem 'chronic'
it does exactly what you ask.