Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery trigger a DatePicker change event

I have the following code:

$('.custom_datepicker_selector').datepicker({
  weekStart: 1
})
.on('changeDate', function(en) {
  var correct_format;
  correct_format = en.date.getFullYear() + '-' + ('0' + (en.date.getMonth() + 1)).slice(-2) + '-' + ('0' + en.date.getDate()).slice(-2);
  $('.custom_datepicker_selector').datepicker('hide');
  return $(this).parent().find("input[type=hidden]").val(correct_format);
});

This displays the date format just like I want it to. However it only does so after I click on the datepicker, not initially.

Initially this date is shown:

2013-02-17

and after I click on it I get this:

17/02/2013

How can I display the correct date immediately? (the code above is in the .ready

I created a jsFiddle for this: http://jsfiddle.net/jwxvz/

This was more a rails problem than a javascript:

I followed abu advice and did it like this in rails:

<%= f.input :order_date, :as => :custom_datepicker, :input_html =>  { :value => localize(@client_order.order_date) } %>
like image 873
rept Avatar asked Feb 17 '13 09:02

rept


1 Answers

You can define the default date format also.

Try this out:

$('.custom_datepicker_selector').datepicker({
  weekStart: 1,
  dateFormat: 'dd/mm/yy'
}).on('changeDate', function(en) {
      $('.custom_datepicker_selector').datepicker('hide');
      return $(this).parent().find("input[type=hidden]").val(en);
   });

UPDATE : (important)

I have seen your JSFiddle, you have kept the default value of the textbox as value="2013-02-17" that's why it shows that value at start just remove it.

like image 181
Abubakkar Avatar answered Sep 19 '22 01:09

Abubakkar