Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace the jquery datepicker dateFormat with momentjs parsing and format

For the sake of consistency I would like to be able to switch the parsing/formatting of JQuery UI's datepicker to use momentjs instead. Any thoughts on what might be the best way to do this?

like image 392
Clint Avatar asked Jul 01 '14 00:07

Clint


People also ask

How can change date format in jQuery ui Datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });

How do I change date format in date picker?

Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.

What is format in Datepicker?

By default, the date format of the jQuery UI Datepicker is the US format mm/dd/yy, but we can set it to a custom display format, eg: for European dates dd-mm-yyyy and so on. The solution is to use the date picker dateFormat option.

What is minDate and maxDate in jQuery Datepicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.


1 Answers

So far the best method that I've found to do this is to override the global jquery-ui parseDate and formatDate functions like so:

$.datepicker.parseDate = function(format, value) {
    return moment(value, format).toDate();
};
$.datepicker.formatDate = function (format, value) {
    return moment(value).format(format);
};

Then this nicely allows you to use the usual syntax for attaching a datepicker to a field but the format you specify will instead refer to a momentjs format instead http://momentjs.com/docs/#/parsing/string-format/

$(".selector").datepicker({ dateFormat: "MM-DD-YYYY" });
like image 173
Clint Avatar answered Oct 10 '22 02:10

Clint