简体   繁体   English

Ruby on Rails - 如何以我需要的格式显示日期? 从YYYY-MM-DD HH:MM:SS UTC转换为MM / DD / YYYY

[英]Ruby on Rails - how to display a date in format i need? Converting from YYYY-MM-DD HH:MM:SS UTC to MM/DD/YYYY

I am a RoR newbie. 我是一个RoR新手。 I tried a lot of things, finally came to following: 我尝试了很多东西,最后来到了以下:

<td>
 <%= Date.strptime(request.baseline_start_date, "%Y-%M-%D %H:%M:%S %Z").strftime("%M/%D/%Y")%>
</td>

But this is also giving me an error: 但这也给了我一个错误:

$_ value need to be String (nil given)

But I know that request.baseline_start_date gives me value (tried printing it separately). 但我知道request.baseline_start_date给了我价值(尝试单独打印)。 I don't know which one it is saying as nil given. 我不知道它给的是哪一个。

Any suggestions on how I can achieve format conversion? 关于如何实现格式转换的任何建议?

In Rails you can use the to_time function on a string to convert it into a Date object: 在Rails中,您可以使用字符串上的to_time函数将其转换为Date对象:

'2012-11-14 14:27:46'.to_time.strftime('%B %e at %l:%M %p')

#=> "November 14 at 2:27 PM"

For a handy, interactive reference guide, refer to http://www.foragoodstrftime.com/ 有关便捷的交互式参考指南,请参阅http://www.foragoodstrftime.com/

Date.strptime(
  "2009-04-24 18:33:41 UTC",
  "%Y-%m-%d %H:%M:%S %Z"
).strftime("%m/%d/%Y")
# => "04/24/2009"

I think maybe you just got the capitalization on your format strings wrong. 我想也许你只是把你的格式字符串弄错了。

Check the active support documentation and examples at: http://apidock.com/rails/ActiveSupport/CoreExtensions/DateTime/Conversions/to_formatted_s 查看活动支持文档和示例: http//apidock.com/rails/ActiveSupport/CoreExtensions/DateTime/Conversions/to_formatted_s

Examples 例子

datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0)   # => Tue, 04 Dec 2007 00:00:00 +0000

datetime.to_formatted_s(:db)            # => "2007-12-04 00:00:00"
datetime.to_s(:db)                      # => "2007-12-04 00:00:00"
datetime.to_s(:number)                  # => "20071204000000"
datetime.to_formatted_s(:short)         # => "04 Dec 00:00"
datetime.to_formatted_s(:long)          # => "December 04, 2007 00:00"
datetime.to_formatted_s(:long_ordinal)  # => "December 4th, 2007 00:00"
datetime.to_formatted_s(:rfc822)        # => "Tue, 04 Dec 2007 00:00:00 +0000"

Or if you really want to customise it, define the helper like: 或者,如果您真的想要自定义它,请定义帮助器,如:

def custom_format(time)
  Time::DATE_FORMATS[:w3cdtf] = lambda { |time| time.strftime("%Y-%m-%dT%H:%M:%S# {time.formatted_offset}") }
end

Ive written a really nice gem that simplifies the whole process, and makes date formatting DRY. 我写了一个非常好的宝石,简化了整个过程,并使日期格式化DRY。

Check it out at: http://github.com/platform45/easy_dates 请查看: http//github.com/platform45/easy_dates

You can use the String#to_time (or Date#to_time) function in ActiveSupport to convert the string into a Time (or Date) object. 您可以使用ActiveSupport中的String#to_time(或Date#to_time)函数将字符串转换为Time(或Date)对象。 Then use strftime as you have already. 然后像你一样使用strftime。

What I have done is add an initializer named conversions.rb in config/initializer After that Add a line like follows: 我所做的是在config / initializer中添加一个名为conversions.rb的初始化程序。之后添加如下所示的行:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:<name> => '<formatting>')

From there on you can render your datetime using your format with: 从那里开始,您可以使用以下格式渲染日期时间:

dateVar.to_s(:<name>)

There is a handy list here of the formatting tokens 有一个方便的名单这里的格式令牌

Thanks a lot for the reply. 非常感谢您的回复。 My problem is, the output seems to be already string and i have to convert from date in string to another format. 我的问题是,输出似乎已经是字符串,我必须从字符串中的日期转换为另一种格式。

When I look at the date stored in database (Oracle) it is mm/dd/yy, but when i get it displayed, it adds the timestamp and timezone. 当我查看存储在数据库(Oracle)中的日期时,它是mm / dd / yy,但是当我显示它时,它会添加时间戳和时区。

I tried setting the default in Configuration\\environment.rb as ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!( :default => '%d %b %Y' ) But that also doesn't seem to help. 我尝试将Configuration \\ environment.rb中的默认值设置为ActiveSupport :: CoreExtensions :: Date :: Conversions :: DATE_FORMATS.merge!(:default =>'%d%b%Y')但是这似乎也没有救命。

At the end, if I just get the string to convert from Timezone format to mm/dd/yyyy, that is enough. 最后,如果我只是将字符串从时区格式转换为mm / dd / yyyy,那就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM