1

我很难解决这个问题......

我正在运行一个带有投票功能的应用程序,我希望投票从“下一个中午”开始并持续 24 小时,而不是让一个新想法从 created_at 日期开始持续 24 小时。

即,如果您在周三晚上 8:00 提出了一个想法,人们可以在周五中午(下一个中午后 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 %>

有点笨拙,但我会在以后清理它。

4

4 回答 4

2

我有一个类似的问题,这就是我使用的:

1. DateTime.tomorrow +12.hours
2. DateTime.tomorrow + 1.day + 12.hours
于 2014-10-16T22:05:50.903 回答
1

你可以像这样找到结束日的中午

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))

尽管该查询没有太大意义,因为从现在到将来的某个时间之间不会产生任何想法。我们仍在等待这些想法的出现。

于 2011-09-12T05:49:55.943 回答
0

Here's a clean way to find the next "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?
于 2012-01-22T02:12:40.283 回答
0

或使用慢性

time = Chronic.parse "noon tomorrow"
于 2012-01-22T03:30:21.650 回答