如何编写查询以将当前日期与数据库中的created_at时间戳进行比较? [英] How to write a query to compare current date to created_at timestamps in database?
本文介绍了如何编写查询以将当前日期与数据库中的created_at时间戳进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个查询,以使用ActiveRecord将时间戳记与created_at时间戳与Ruby on Rails中的当前日期进行比较,我还不知道怎么做
I would like to write a query to compare created_at timestamp with my current date in Ruby on Rails using ActiveRecord and I don't quite know how do it yet
日期存储如下-> created_at:2019-06-03 03:30:40
Date is stored like this -> created_at: 2019-06-03 03:30:40
推荐答案
尝试一下:
<% if @user.created_at > 5.days.ago.beginning_of_day %>
<span>New User!</span>
<% end%>
或者您可以使用:
<%= time_ago_in_words(@user.created_at) %>
来源: https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
这篇关于如何编写查询以将当前日期与数据库中的created_at时间戳进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文