我在Rails 4应用程序中使用time_diff
gem。我想显示created_at
字段与created_at + 7.days
之间剩余的天/小时/分钟/秒。
Time.diff(@auction.created_at + 7.days, @auction.created_at, '%d %H %N %S')[:diff]
输出:
7 days 0 hours 0 minutes 0 seconds
我如何得到如下结果:
7d 0h 0m 0s
有什么建议吗?
答案 0 :(得分:1)
根据他们的文档,您可以使用%h %m %s
代替%H %N %S
文档摘录:
%h - hour (without adding 'hour' text to the hours. eg: 3 for 3 hours)
%m - minute (without adding 'minute' text)
%s - second (without adding 'second' text)
所以你的代码应该是
Time.diff(@auction.created_at + 7.days, @auction.created_at, '%d %hh %mm %ss')[:diff]
现在您需要使用i18n yaml文件
en:
day: "d"