Using jQuery datePicker, I set minDate to "-3y".
When I call:
$mydatepicker.datepicker("option", "minDate")
I get
-3y
Is it possible using datePicker methods to get it as mm/dd/yyyy?
Using jQuery datePicker, I set minDate to "-3y".
When I call:
$mydatepicker.datepicker("option", "minDate")
I get
-3y
Is it possible using datePicker methods to get it as mm/dd/yyyy?
Unfortunately datepicker does not offer a public API that handles relative dates, just internal methods that do that.
Consider calculating the minDate server side - that is what I ended up doing.
If you still absolutely need it:
// DO NOT USE THIS IN PRODUCTION CODE!!
var minDate = $.datepicker._getMinMaxDate( $mydatepicker.data('datepicker'), 'min' ),
zeroPad = function( toPad, padToLength ) {
toPad = toPad.toString();
var i = toPad.length;
while( i < padToLength )
toPad = '0' + toPad, i++;
return toPad;
}
formattedMinDate = zeroPad( minDate.getMonth(), 2 ) + '/'
zeroPad( minDate.getDay(), 2 ) + '/'
minDate.getFullYear();
Please note that the code above is -bad- and you should not be using it as it relies on private methods that might disappear without notice in the future.
You should do
var date = $mydatepicker.datepicker({ dateFormat: 'dd-mm-yy' }).val();
Alternatively, you can also do:
var date = $mydatepicker.datepicker('getDate');
$.datepicker.formatDate('dd-mm-yy', date);
If you want it as an object, you can remove the .val()
More Info available at http://docs.jquery.com/UI/Datepicker/formatDate and jQuery UI DatePicker - Change Date Format