0

This is what I have in my .js file:

var ready;
ready = function () {
    $.datepicker.setDefaults({
        dateFormat: 'yy-mm-dd'
    });
};
$(document).ready(ready);

But datepicker format is still the default: mm/dd/yy

Background: - I'm on Ruby on Rails - also using bootstrap-datepicker-rails gem. Is there a conflict?

acrobat
  • 2,179
  • 2
  • 27
  • 35
Pod Mays
  • 2,563
  • 7
  • 31
  • 44
  • Check http://stackoverflow.com/questions/1328025/jquery-ui-datepicker-change-date-format – ahPo Oct 23 '15 at 00:48
  • You'll need to call the function, just defining it doesn't cause it to be run. You can add `()` to the end of it's definition to run it, but it would probably be better to not run it until the document is ready. `$(function() { your code });` – dinjas Oct 23 '15 at 00:48

3 Answers3

1

Took a quick look into jQuery Datepicker api documentation and I found out you can do this

$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
akbarbin
  • 4,985
  • 1
  • 28
  • 31
jkris
  • 5,851
  • 1
  • 22
  • 30
1

It might be possible that your code is being run after the datepicker is already initialized. You should be able to pass the dateFormat: "yy-mm-dd" option in wherever your datepicker is being initialized in your JavaScript.

You should be able to update the dateFormat after it's been initialized (based on these docs) like this:

// Setter
$(".selector").datepicker("option", "dateFormat", "yy-mm-dd");

.selector is most likely .datepicker in your case unless you've changed it (going of the examples here).

Also, you should be able to see what the date format is set to like this:

console.log("dateFormat:", $(".selector").datepicker("option", "dateFormat"));
dinjas
  • 2,115
  • 18
  • 23
0

try like this:

$.extend($.fn.datepicker.defaults, {
    format:'yyyy-mm-dd',
    autoclose: true,
    todayHighlight: true,
    showOnFocus: false
});
Baron
  • 379
  • 3
  • 9