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?
inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });
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.
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.
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.
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" });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With