[英]Rails - format date_field
how can I style the date format of a date_field?如何设置 date_field 的日期格式?
I got the following form:我得到了以下表格:
<%= form_for([@user, @vehicle]) do |f| %>
<%= f.label :name, "Vehicle name" %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :matriculation_date %>
<%= f.date_field :matriculation_date, class: 'form-control' %>
<%= f.submit "Add", class: "btn btn-primary" %>
<% end %>
this is rendering the following HTML for the date field:这是为日期字段呈现以下 HTML:
<input class="form-control" type="date" name="vehicle[matriculation_date]" id="vehicle_matriculation_date">
The date field accept input in the following format: mm/dd/yyyy
.日期字段接受以下格式的输入: mm/dd/yyyy
。 I want it to accept format like dd/mm/yyyy
.我希望它接受像dd/mm/yyyy
这样的格式。
I tried to edit it.yml file without any luck.我试图编辑 it.yml 文件但没有任何运气。
it:
date:
formats:
default: ! '%d/%m/%Y'
datetime:
formats:
default: ! '%d/%m/%Y %H:%M'
Any clue?有什么线索吗? Thank you谢谢
None of the proposed solutions solve my problem, so I think my question was not very clear.提出的解决方案都没有解决我的问题,所以我认为我的问题不是很清楚。 What I want is just to style how the date_field prompt the user (see image for more details).我想要的只是设置 date_field 如何提示用户的样式(有关更多详细信息,请参见图像)。 At the moment the form show mm/dd/yyyy
, and I want it to show dd/mm/yyyy
.目前表单显示mm/dd/yyyy
,我希望它显示dd/mm/yyyy
。
Not sure it might be a useful information, but I'm using Bootstrap for styling.不确定它可能是有用的信息,但我使用 Bootstrap 进行样式设置。
你可以这样做:
<%= f.date_field :matriculation_date, as: :date, value: f.object.try(:strftime,"%m/%d/%Y"), class: 'form-control' %>
You can try this你可以试试这个
@yourdate.strftime("%m/%d/%Y")
this works!这有效!
You can convert the date formate with strftime()
您可以使用strftime()
转换日期strftime()
@date.strftime("%d/%m/%Y")
It will convert your date into dd/mm/yyyy
formate as you wanted.它会根据您的需要将您的日期转换为dd/mm/yyyy
格式。 Here I am showing you an example:在这里,我向您展示一个示例:
t = Time.now
=> 2016-04-28 16:09:42 -0700
>> t.strftime("%d/%m/%Y")
=> "28/04/2016"
for more strftime()
formatting options you can check this http://apidock.com/ruby/DateTime/strftime有关更多strftime()
格式选项,您可以查看此http://apidock.com/ruby/DateTime/strftime
EDIT :编辑:
After reading your comment and screenshot i've got the solution as follow.阅读您的评论和屏幕截图后,我得到了如下解决方案。
You can also use this gem: https://github.com/Nerian/bootstrap-datepicker-rails on your gemfile.rb你也可以在你的 gemfile.rb 上使用这个 gem: https : //github.com/Nerian/bootstrap-datepicker-rails
gem 'bootstrap-datepicker-rails'
then bundle install and restart rails server然后捆绑安装并重新启动rails服务器
then add this line to app/assets/stylesheets/application.css然后将此行添加到app/assets/stylesheets/application.css
*= require bootstrap-datepicker
and add this line to app/assets/javascripts/application.js并将这一行添加到app/assets/javascripts/application.js
//= require bootstrap-datepicker
and to use this in your form:并在您的表单中使用它:
<%= f.date_field :matriculation_date, :id => "datepicker" %>
and add this javascript on the same view of your form并在表单的同一视图中添加此 javascript
<script>
$('#datepicker').datepicker({format: 'dd/mm/yyyy'});
</script>
My answer is based on @Dharmesh_Rupani answer我的回答基于@Dharmesh_Rupani 的回答
Follow the steps to add the gem 'bootstrap-datepicker-rails'
按照步骤添加gem 'bootstrap-datepicker-rails'
I made two little changes on the form我对表格做了两个小改动
<%= f.text_field : matriculation_date, :placeholder => 'dd/mm/yyyy', class: 'form-control datepicker' %>
The first thing is I changed the type of the field to text, because when I implemented I had two calendars, one from the date_field and another from the new gem第一件事是我将字段的类型更改为文本,因为当我实现时我有两个日历,一个来自 date_field,另一个来自新 gem
The other thing I changed is instead of :id
I used :class
, because it may happen that you have more than one date field我更改的另一件事是我使用:class
而不是:id
,因为可能会发生您有多个日期字段
lastly, you should add this on the view最后,你应该在视图中添加这个
<script>
$( document ).ready(function() {
$('.datepicker').datepicker({format: 'dd/mm/yyyy'});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.