[英]How to change date format from yyyy/mm/dd to dd/mm/yyyy Ruby on Rails
<div class="field2">
<%= f.label :shod_date, "Date of last shoeing" %><br />
<%= f.date_select :shod_date %>
Can someone please just tell me what codes to add and where i should add them. 有人可以告诉我要添加什么代码以及应该在哪里添加它们。 Also which commands i should run.
还有我应该运行哪些命令。 Plus i want the initial year to date back further than 2007 because the start year starts at 2007 and ends at 2012
另外,我希望初始年份可以追溯到2007年,因为起始年份始于2007年,结束于2012年
thank you. 谢谢。
Try this: 尝试这个:
<%= f.date_select :shod_date,
use_month_numbers: true,
order: [:day, :month, :year],
start_year: 2007,
date_separator: "/" %>
使用shod_date.strftime(“%d-%m-%Y”)
You can change the date format using internationalisation (I18n) 您可以使用国际化(I18n)更改日期格式
Just add (or change) this in your config/locales/en.yml
: 只需在您的
config/locales/en.yml
添加(或更改)它:
en:
date:
order:
- :day
- :month
- :year
Note that this example is for english ( en
) locale, and you should replace en
for your language. 请注意,此示例适用于英语(
en
)语言环境,应将en
替换为您的语言。
Using i18n, all your date_select
will be setted with this format. 使用i18n,您所有的
date_select
都将以此格式设置。 If you want change the order only in this point, use the attribute order: [:day, :month, :year]
as @hirata-yasuyuki sugested. 如果您只想在此时更改顺序,请使用属性
order: [:day, :month, :year]
因为@ hirata-yasuyuki表示为Sugested。
To display only years between 2007 and current you can set two attributes: start_year
and end_year
to date_select
helper. 要仅显示2007年到当前之间的年份,可以设置两个属性:
start_year
和end_year
为date_select
helper。
And your code show looks like: 您的代码显示如下:
<%= f.date_select :shod_date, start_year: 2007, end_year: Time.now.year %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.