【问题标题】:Best way to manage date objects?管理日期对象的最佳方法?
【发布时间】:2010-08-25 14:09:14
【问题描述】:

我有一些日期对象是 Date 类的 Date,还有一些是 Time。

问题在于,当我解析它们以按时间顺序加载它们时,它们首先列为时间,然后是日期。

这是加载它们的代码:

  query.any_of do |any_of|
    any_of.with(:date).greater_than((params[:current_date] || DateTime.now))
    any_of.with(:end_date).greater_than((params[:current_date] || DateTime.now))
    any_of.with(:time).greater_than((params[:current_date] || DateTime.now))
  end

有没有办法将日期和时间组合为一个通用对象来比较它们?理想情况下,它将日期对象转换为时间,因为时间对象可以保存日期和时间。

【问题讨论】:

    标签: ruby-on-rails ruby datetime date time


    【解决方案1】:
    def universal_time
      if self.date != nil
        self.date.to_time
      else
        self.time
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-30
      • 2012-01-22
      • 2017-03-01
      • 2014-01-11
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多