在Ruby on Rails中将DateTime转换为简单日期 [英] Transform DateTime into simple Date in Ruby on Rails

查看:573
本文介绍了在Ruby on Rails中将DateTime转换为简单日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何做到这一点?

我有一个数据库中的datetime列,我想将其转换成一个简单的日期。 >

 
def shown_date
#to_date不存在,但是我正在寻找
self.date || self.exif_date_time_original.to_date
end


解决方案

DateTime# to_date 与ActiveSupport存在:

  $ irb 
>> DateTime.new.to_date
NoMethodError:未定义的方法'to_date'for#< DateTime:-1 / 2,0,2299161>来自(irb)的
:1

>>需要'active_support / core_ext'
=> true

>> DateTime.new.to_date
=>星期一,01一月-4712


I have a datetime column in db that I want to transform into a simple date when I show it to users.

How can I do that?

def shown_date
  # to_date does not exist, but is what I am looking for
  self.date || self.exif_date_time_original.to_date 
end

解决方案

DateTime#to_date does exist with 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

这篇关于在Ruby on Rails中将DateTime转换为简单日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆