更改 Rails 4 中的默认日期和时间格式 [英] Changing the default date and time format in Rails 4

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

问题描述

我一直在寻找一种方法来更改 Rails 4 中的默认日期格式.

I was looking for a way to change the default date format in Rails 4.

推荐答案

通过 Rails 国际化 (I18n) API

可以通过将格式添加到 i18n 配置来转换"数据和时间格式.

Data and time formats can be 'translated' by adding the format to the i18n configuration.

config/locales/en.yml

config/locales/en.yml

en:
  date:
    formats:
      default: "%d/%m/%Y"
  time:
    formats:
      default: "%d/%m/%Y %H:%M"

注意:记住不要有缩进的制表符,就像我第一次那样:)

Note: remember to not have tabs for the indent, like I did first time :)

正如 NoelProf 在评论中提到的

As mentioned by NoelProf in the comments

要使用 i18n 转换,请不要忘记在您之前的 l(小写 L)在视图中约会!例如:<%= l your_date %>

To use i18n conversion don't forget the l (lower case L) before your date in views! For example: <%= l your_date %>

如果您发现其他有效的方法,欢迎您发表评论.

You are invited to comment if you found other ways working well.

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

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