我很难理解这个问题…
我正在运行一个带有投票的应用程序,而不是让一个新的想法从created_at日期持续24小时,我希望投票在"下一个中午"开始并持续24小时。
。如果你在周三晚上8点提出了一个想法,人们可以在周五中午之前对这个想法进行投票(下一个中午之后的24小时)。或者,如果你在周四上午11点50分提出了一个想法,你也可以在周五中午之前投票。
基本上我只需要找到一种方法来创建一个实例。说:
@recentideas = current.user.ideas.where(:created_at => (nextnoontime)...(nextnoontime + 24.hours))
我承认上面不是实际的代码,但只是把它放在一个角度…
提前感谢。
更新解决方案:
更简单的解决方案(不是很优雅)是这样说:
<% if idea.created_at.hour < 12 %>
<% @nextnoon = idea.created_at.midnight + 12.hours %>
<% @timedif = @nextnoon - idea.created_at %>
<% else %>
<% @nextnoon = idea.created_at.tomorrow.midnight + 12.hours %>
<% @timedif = ((@nextnoon - 12.hours) - idea.created_at) + 12.hours %>
<%end%>
然后相关的if语句是:
<% if idea.created_at > ((24.hours + @timedif)/3600).hours.ago %>
有点笨拙,但我会在以后的日期清理它。
我有一个类似的问题,这是我使用的:
1. DateTime.tomorrow +12.hours
2. DateTime.tomorrow + 1.day + 12.hours
你可以找到结束一天的中午,像这样
now = DateTime.now
twelve_hours_from_now = now + 12.hours
beginning_of_day = DateTime.new(twelve_hours_from_now.year, twelve_hours_from_now.month, twelve_hours_from_now.day)
noon_of_ending_day = beginning_of_day + 36.hours
所以你的查询应该是
@recentideas = current.user.ideas.where(:created_at => (now...noon_of_ending_day))
虽然这个查询没有太大意义,因为从现在到未来的某个时间不会产生任何想法。我们仍在等待这些想法的出现。
这里有一个简单的方法来查找下一个"noon"
# Today
time = DateTime.now
# Noon, today
time += (12 - time.hour).hours
time -= time.min.minutes
time -= time.sec.seconds
# Noon tomorrow, if noon today is in the past
time += 1.day unless time.future?
或者使用慢性病:
time = Chronic.parse "noon tomorrow"