0

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????

Erin Walker
  • 739
  • 1
  • 11
  • 30
  • `dateFormat` works fine without coffee. http://jsfiddle.net/y2wgK/ Looks like coffee is not placing options object correctly. I don't use coffee so is likely a syntax problem – charlietfl Apr 21 '13 at 12:24
  • Tried this and still doing the same: $(function (){ $('#post_date').datepicker() ({ dateFormat: 'dd-mm-yy' }); }); – Erin Walker Apr 21 '13 at 13:47
  • It seems rails is ignoring everything I put in after .datepicker => what am I doing wrong?? – Erin Walker Apr 21 '13 at 14:22

3 Answers3

4

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"
Community
  • 1
  • 1
amb110395
  • 1,545
  • 13
  • 16
  • It seems everything I put after .datepicker is ignored by Rails? What am I missing? – Erin Walker Apr 21 '13 at 14:30
  • Then it seems to me that there is probably be a syntax error in your CoffeeScript file. Can you edit your post and add your code? – amb110395 Apr 21 '13 at 15:27
  • All the code is there including the full CoffeeScript file. I added the minDate too. I have looked, tested, googled everywhere now. I'm stumped! – Erin Walker Apr 21 '13 at 15:41
  • It seems to me that the CoffeeScript is correct but I don't have much experience with it really. Instead of using CoffeeScript why don't you use plain javascript? – amb110395 Apr 21 '13 at 16:09
  • Tried that as well. Same thing! – Erin Walker Apr 21 '13 at 17:46
  • Hmmm. This is interesting. There doesn't seem to be any mistake with the javascript itself. The first thing that comes to mind then is that javascript is not being included in your application and executed. Check that. Also, it would help if you put the whole javascript file, not only the datepicker part. Maybe there is an unrelated error that is causing this problem. Also, show me the plain javascript you used. Maybe you made a mistake with that? To tell you the truth this should be a rather straightforward thing, as charlietfl showed in his jfiddle. I'm not really sure why it isn't working. – amb110395 Apr 21 '13 at 18:59
  • Found it! Such a stupid problem: Coffee should be: $('#post_date').datepicker (dateFormat: 'dd-mm-yy') – Erin Walker Apr 21 '13 at 19:01
  • Also, check the following link: http://jqueryui.com/datepicker/#date-formats which is the official tutorial on how to change the date format. – amb110395 Apr 21 '13 at 19:02
  • Glad to hear that. I'll update my answer for future reference. – amb110395 Apr 21 '13 at 19:03
0

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.

Erin Walker
  • 739
  • 1
  • 11
  • 30
0

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)

Jerome
  • 5,583
  • 3
  • 33
  • 76