给定一个时间戳,如何确定几周前?(Given a timestamp, how to determine how many weeks ago?)

编程入门 行业动态 更新时间:2024-10-28 18:24:01
给定一个时间戳,如何确定几周前?(Given a timestamp, how to determine how many weeks ago?)

我希望返回自我的应用程序中创建用户以来的周数。

我的模型是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:00

How 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_weeks

If 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.week

If 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

更多推荐

本文发布于:2023-07-22 01:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215543.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几周   时间   timestamp   determine   weeks

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!