【问题标题】:Transform DateTime into simple Date in Ruby on Rails在 Ruby on Rails 中将 DateTime 转换为简单的 Date
【发布时间】:2010-11-09 18:31:44
【问题描述】:

我在 db 中有一个 datetime 列,当我将它显示给用户时,我想将其转换为一个简单的日期。

我该怎么做?

def 显示日期 # to_date 不存在,但正是我要找的 自我.日期 || self.exif_date_time_original.to_date 结尾

【问题讨论】:

  • 你只是想格式化它吗?
  • 不,我想创建一个 Date 对象
  • 哎呀..愚蠢的问题! to_date 确实存在!对不起:(

标签: ruby-on-rails ruby datetime


【解决方案1】:

DateTime#to_date 确实存在于 ActiveSupport 中:

$ irb
>> DateTime.new.to_date
NoMethodError: undefined method 'to_date' for #<DateTime: -1/2,0,2299161>
    from (irb):1

>> require 'active_support/core_ext'
=> true

>> DateTime.new.to_date
=> Mon, 01 Jan -4712

【讨论】:

  • 很遗憾.to_date() 不存在于标准 ruby​​ 中,因此我们必须采取解决方法。使用正确的时区槽字符串 (!!!) 从 DateTime 转换为 Date 并不优雅。了解 DateTime 和 Date 的内部结构并不容易。暴跳如雷,无视! :-)
  • @Notinlist 整个 rails 都是用 Ruby 编写的,因此您可能可以查看 rails api 以找到 to_date() 方法。这将告诉您它所在的模块,然后您可以在代码中包含 DateTime 对象
  • .to_date 是在 Ruby 1.9.2 中添加的。
  • @Michael 你不正确。这是 Ruby 1.9.2 标准库的文档,其中记录了 to_date 函数。 ruby-doc.org/stdlib-1.9.2/libdoc/date/rdoc/…
【解决方案2】:

你好 collimarco :) 你可以使用 ruby​​ strftime 方法来创建你自己的日期/时间显示。

time.strftime(string) => 字符串

  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character

   t = Time.now
   t.strftime("Printed on %m/%d/%Y")   #=> "Printed on 04/09/2003"
   t.strftime("at %I:%M%p")            #=> "at 08:56AM"

【讨论】:

    【解决方案3】:

    我最近写了一个 gem 来简化这个过程并整理你的观点等等。

    查看:http://github.com/platform45/easy_dates

    【讨论】:

      【解决方案4】:

      在 Ruby 1.9.2 及更高版本中,他们向 DateTime 添加了一个 .to_date 函数:

      http://ruby-doc.org/stdlib-1.9.2/libdoc/date/rdoc/DateTime.html#method-i-to_date

      此实例方法似乎在 1.8.7 等早期版本中不存在。

      【讨论】:

      • 仅当您包含我详细说明的积极支持时。
      • 不,在 Ruby 1.9.2 及更高版本中,您不需要包含 ActiveSupport。正如您在我的回答中看到的那样,我包含了 1.9.2 核心 Ruby stdlib 文档的链接。
      【解决方案5】:

      先尝试将条目转换为字符串。只要数据库列类型是日期,它将被格式化为日期。

      self.date || self.exif_date_time_original.to_s

      【讨论】:

        【解决方案6】:

        对于旧的 Ruby (1.8.x):

        myDate = Date.parse(myDateTime.to_s)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-04-02
          • 2016-12-23
          • 2011-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多