我希望返回自我的应用程序中创建用户以来的周数。
我的模型是User.rb(id,created_at)
给定user.created_at :
2.4.0 :008 > user.created_at => Mon, 14 Aug 2017 15:51:23 UTC +00:00我该怎么做: user.created_at.weeks_ago
返回的是一些整数,表示自用户创建以来的周数?
谢谢
I'm looking to return the number of weeks since a user was created in my app..
My model is User.rb (id, created_at)
Given user.created_at:
2.4.0 :008 > user.created_at => Mon, 14 Aug 2017 15:51:23 UTC +00:00How can I do something like: user.created_at.weeks_ago where it returns some integer which represents the number of weeks since the user was created?
最满意答案
如果您只想构建一个人性化的字符串,我相信time_ago_in_words就是您所追求的。
https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
否则,我会采取以下措施:
(Time.new - user.created_at) / 1.week如果你想要一个更优雅的解决方案, 在Ruby中减去日期,并在几分钟内得到差异,提到Ruby的时差宝石 。
例如
TimeDifference.between(user.created_at.to_time, Time.now).in_weeksIf you just want to build a human-friendly string, I believe time_ago_in_words is what you are after.
https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
Otherwise, I would resort to something like the following:
(Time.new - user.created_at) / 1.weekIf you want a more elegant solution, Subtract dates in Ruby and get the difference in minutes mentions the Time Difference gem for Ruby.
e.g.
TimeDifference.between(user.created_at.to_time, Time.now).in_weeks更多推荐
发布评论