如何在达到某个日期时间之前返回天数和小时数?

时间:2022-05-06 21:26:39

I am building a daily deal application to learn Ruby on Rails.

我正在构建一个日常交易应用程序来学习Ruby on Rails。

I'd like to display for each deal how long until the deal is no longer available in days AND hours.

我想为每笔交易显示多长时间,直到交易在几天和几小时内不再可用。

I am now using in my view:

我现在在我的观点中使用:

Ends in : <%= distance_of_time_in_words_to_now(game.game_end_date) %>

If there are only a few hours left, it works well and returns for example "deal ends in 3 hours".

如果只剩下几个小时,它运作良好并返回例如“交易在3小时内结束”。

When there is still more than ONE day to go the method returns "deal ends in about 2 days". I would like to display something more precise in that case with the number of days AND hours. For example, it should return "deal ends in 2 days and 7 hours".

当还有一天以上时,该方法返回“交易在约2天内结束”。在这种情况下,我希望用天数和小时数来显示更精确的内容。例如,它应该返回“交易在2天7小时内结束”。

Is there a Rails method for this?

这有Rails方法吗?

1 个解决方案

#1


3  

You can use the gem dotiw that has more options than the classic distance_of_time_in_words:

您可以使用包含比经典distance_of_time_in_words更多选项的gem dotiw:

<%= distance_of_time_in_words(Time.now, game.game_end_date, false, :only => ["days", "hours"]) %>

#1


3  

You can use the gem dotiw that has more options than the classic distance_of_time_in_words:

您可以使用包含比经典distance_of_time_in_words更多选项的gem dotiw:

<%= distance_of_time_in_words(Time.now, game.game_end_date, false, :only => ["days", "hours"]) %>