简体   繁体   English

如何将日期格式从yyyy / mm / dd更改为dd / mm / yyyy Ruby on Rails

[英]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_yearend_yeardate_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.

相关问题 如何在rails上的ruby中将日期格式从dd-mmm-yyyy转换为yyyy-mm-dd - how to convert the date format from dd-mmm-yyyy to yyyy-mm-dd in ruby on rails Ruby on Rails - 如何以我需要的格式显示日期? 从YYYY-MM-DD HH:MM:SS UTC转换为MM / DD / YYYY - Ruby on Rails - how to display a date in format i need? Converting from YYYY-MM-DD HH:MM:SS UTC to MM/DD/YYYY 如何在Ruby中以dd-mm-yyyy格式显示出生日期 - How to display date of birth in dd-mm-yyyy format in Ruby 如何在Rails中以“mm / dd / yyyy”格式显示当前日期 - How to display the current date in “mm/dd/yyyy” format in Rails Rails日期时间格式“dd / mm / yyyy” - Rails datetime format “dd/mm/yyyy” 让轨道接受欧洲日期格式(年/月/日) - Getting rails to accept European date format (dd/mm/yyyy) rails-以字符串形式获取YYYY-MM-dd日期格式 - rails - get YYYY-MM-dd date format in string Rails 3,MySQL Production只允许以mm / dd / yyyy或yyyy-mm-dd格式的日期输入dd / mm / yyyy - Rails 3, MySQL Production only allows dates in mm/dd/yyyy or yyyy-mm-dd format need to input in dd/mm/yyyy 如何在文本字段中显示格式为“dd / mm / YYYY”的日期? - How to display in a text field a date with format “dd/mm/YYYY”? 如何使用Ruby on Rails将格式为09-feb-73的日期转换为1973年2月9日(mm / dd / yyyy) - How to convert date in format 09-feb-73 to 02/09/1973 (mm/dd/yyyy) using Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM