Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default date format for datepicker?

I have a page where I keep track of various dates. So I end up using datepicker through jquery anywhere from 10-200 times. I want to set the default for all cases of datepicker to the 'yy-mm-dd' - but I can't seem to figure out how to set that?

like image 430
Sol Orwell Avatar asked Feb 22 '12 19:02

Sol Orwell


People also ask

How do I initialize a date picker?

Initialize the datepicker and specify the date format in "yyyy-mm-dd". JavaScript Code : $( "#datepicker" ). datepicker(); $( "#datepicker" ).

How do I change the input date format?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.


1 Answers

I end up using datepicker through jquery anywhere from 10-200 times.

I'm assuming you're calling datepicker() separately for each input, probably by id. There's no need for that.

Use a class as a hook on your input:

<input class="my-datepicker">

Then call datepicker on elements with that class with your default configuration:

$('.my-datepicker').datepicker({
    dateFormat: 'yy-mm-dd'
});

When you need a different configuration, use/assign a different class name.

You can also use setDefaults (probably what you're looking for):

$.datepicker.setDefaults({
    dateFormat: 'yy-mm-dd'
});
like image 60
Wesley Murch Avatar answered Nov 03 '22 11:11

Wesley Murch