Pod Mays
Pod Mays

Reputation: 2583

jQuery datepicker default format not working

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?

Upvotes: 0

Views: 1344

Answers (3)

Baron
Baron

Reputation: 379

try like this:

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

Upvotes: 0

jkris
jkris

Reputation: 6511

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

$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });

Upvotes: 1

dinjas
dinjas

Reputation: 2125

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"));

Upvotes: 1

Related Questions