简体   繁体   English

Ruby on Rails中的内联数据格式

[英]Inline data formatting in Ruby on Rails

Within a rails app I have a .jst.eco template which includes the line... 在Rails应用程序中,我有一个.jst.eco模板,其中包括该行...

<span> <%= @event.get 'create_at' %> </span> 

...which renders the date like so 2015-01-17 11:04:03. ...这样呈现日期,就像2015年1月17日11:04:03。

However I only want to display 2015-01-17. 但是我只想显示2015-01-17。 Is there a neat formatting trick I can use within the <%= ... %>? 我可以在<%= ...%>中使用整洁的格式化技巧吗?

You can write as: 您可以这样写:

span> <%= @event.get('create_at').to_s(:db) %> </span>
# or
span> <%= @event.get('create_at').to_formatted_s(:db) %> </span>

:db means '%Y-%m-%d . :db表示'%Y-%m-%d Check out DATE_FORMATS . DATE_FORMATS

It depends on the object you have. 这取决于您拥有的对象。 If it is a time object, you can do: 如果是时间对象,则可以执行以下操作:

@event.get('create_at').strftime('%F')

if it is just a string, then first you should parse it: 如果只是一个字符串,那么首先您应该解析它:

Time.parse(@event.get('create_at')).strftime('%F')

According to .strftime docs %F is The ISO 8601 date format (%Y-%m-%d) 根据.strftime文档, %FThe ISO 8601 date format (%Y-%m-%d)

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

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