你如何在 Rails 中做相对时间? [英] How do you do relative time in Rails?

查看:37
本文介绍了你如何在 Rails 中做相对时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Rails 应用程序,但似乎无法找到如何做相对时间,即如果给定某个时间类,它可以计算30 秒前"或2 天前"或更长的时间超过一个月9/1/2008"等

I'm writing a Rails application, but can't seem to find how to do relative time, i.e. if given a certain Time class, it can calculate "30 seconds ago" or "2 days ago" or if it's longer than a month "9/1/2008", etc.

推荐答案

听起来您正在寻找 time_ago_in_words 方法(或 distance_of_time_in_words),来自 ActiveSupport.像这样称呼它:

Sounds like you're looking for the time_ago_in_words method (or distance_of_time_in_words), from ActiveSupport. Call it like this:

<%= time_ago_in_words(timestamp) %>

这篇关于你如何在 Rails 中做相对时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆