How to Change the Date formatting with jQuery UI Datepicker and Rails?

ε祈祈猫儿з 提交于 2019-12-12 03:55:55

问题


I can't believe how complicated the date picker can be. I have looked at various questions on Stackoverflow, but can't get the formatting to work. I used the Railscasts #213 Calendars (revised) to set up the jQuery Datepicker.

Here in South Africa we use the dd/mm/yyyy formating and not the mm/dd/yyyy formatting as everyone says.

First problem is when I pick the date it shows up on the form as mm/dd/yyyy. I can't change this - tried changing the Coffee Script, but that doesn't do anything:

  jQuery ->
   $('#post_date').datepicker()
   dateFormat: "dd/mm/yy"
   minDate: "1w"

After it posted it shows up as mm/dd/yyyy, but now I want the date to show up in words and with the folowing code 1 April 2013 comes out as 4 January 2013.

        <%= @post.date.strftime("%b %d %Y") %>

Ive tried installing I18n and setting the default on the en.yml, but that has no effect:

        date:
         formats:
          default: "%d/%m/%y"

Please can someone please give a straight answer as to how to do this????


回答1:


I can't help you with the jQuery datepicker as I have never used it. Maybe this post here is what you need: jQuery UI DatePicker - Change Date Format

Basically, this should solve your problem:

$('#post_date').datepicker(dateFormat: 'dd-mm-yy');

As far as formatting the date when printing it, instead of using strftime to do so you can quickly set a universal default format for your dates by creating date_formats.rb in the config/initializers folder in your app and adding this line:

Date::DATE_FORMATS[:default] = "%d-%m-%Y"



回答2:


The Coffee script should be (see brackets):

  $('#post_date').datepicker (dateFormat: 'dd-mm-yy')

I don't know how the Railscasts #213 Calendars (revised) works, because Ryan doesn't use the brackets at all.




回答3:


I realize this is late, but enter link description here helps format the data in the form you're looking for.

I had to resort to direct wiring the jquery plugin, as the rails hooks did not work for me (calendar never popped up)



来源:https://stackoverflow.com/questions/16130815/how-to-change-the-date-formatting-with-jquery-ui-datepicker-and-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!