rails 4 中的自定义日期时间格式 [英] Custom date time format in rails 4

查看:54
本文介绍了rails 4 中的自定义日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过调用以下内容在 rails 4 中显示自定义日期格式:

I want to display a custom date format in rails 4 by calling something like:

@reveiw.created_at.to_s(:submitted)

到目前为止我已经尝试过:

So far I have tried:

Time::DATE_FORMATS(:submitted) = "%b %d %Y"

在我的 config/environment.rb 和 config/initializers/time_formats.rb 文件中.

in my config/environment.rb and config/initializers/time_formats.rb files.

我还在我的 config/locales/en.yml 文件中添加了以下代码

I have also added the following code to my config/locales/en.yml file

en:
  time:
    formats:
      submitted: "%b %d %Y"

我没有成功.Rails 4 有明确的策略吗?

I have not had success. Is there a definitive strategy for rails 4?

推荐答案

如果你想用@review.created_at.to_s(:submitted),我觉得你需要用时间::DATE_FORMATS[:submitted] = "%b %d %Y"(使用方括号代替圆括号)

If you want to use @review.created_at.to_s(:submitted), I think you need to use Time::DATE_FORMATS[:submitted] = "%b %d %Y" (use square brackets instead of parentheses)

我相信本地化文件输出格式可以通过 Rails 的 #l 助手 http://guides.rubyonrails.org/i18n.html#adding-date-time-formats.所以 <%= l review.created_at, format: :submitted %> 应该在 erb 视图中工作

I believe the localization file output format is usable through Rails' #l helper http://guides.rubyonrails.org/i18n.html#adding-date-time-formats. So <%= l review.created_at, format: :submitted %> should work in an erb view

这篇关于rails 4 中的自定义日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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