表单字段中的 Rails 日期格式 [英] Rails date format in form field

查看:38
本文介绍了表单字段中的 Rails 日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在文本字段中使用 mm/dd/year 格式的日期.但是,它们目前显示为 2010-03-26.

I'd like my dates in the mm/dd/year format in text fields. However, they currently displays as 2010-03-26.

是否可以设置全局设置来更改此设置?

Is there a global setting I can set to change this?

我尝试了以下操作,这似乎更新了 .to_s 方法,但表单字段保持不变.

I tried the following, which seems to update the .to_s method, but form fields stay the same.

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:default => '%m/%d/%Y')

谢谢

推荐答案

您必须在初始化程序中注册 default 格式.

You have to register the default format in an initializer.

将此行添加到 config/initializers/date_time_formats.rb.

Date::DATE_FORMATS[:default] = '%m/%d/%Y'

# if you want to change the format of Time display then add the line below
Time::DATE_FORMATS[:default]= '%m/%d/%Y %H:%M:%S'

# if you want to change the DB date format.
Time::DATE_FORMATS[:db]= '%m/%d/%Y %H:%M:%S'

现在在 script\console 中测试格式.

Now in the script\console lets test the format.

>> Date.today.to_s
=> "03/14/2010"

>> Time.now.to_s
=> "03/14/2010 13:20:55"

这篇关于表单字段中的 Rails 日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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