I have been searching everywhere but can’t seem to find a solution to this problem.
I need to set the datepickers input value to today’s date minus 2 months.
I am using the following code to display today’s date by default in the input field…
jQuery(function () {
jQuery("#datepicker").datepicker({
dateFormat: 'dd-mm-yy'
});
var day = new Date();
var month = day.getMonth() + 1;
var date = day.getDate() + '-' + month + '-' + day.getFullYear();
jQuery("#datepicker").val(date);
});
Is there a way it can show 2 months prior?
Any help would be much appreciated.
Set the
defaultDateoption: http://jqueryui.com/demos/datepicker/#option-defaultDateHere is a demo: http://jsbin.com/ezuxen/edit#javascript,html,live
Here is a quick run-down of some of the changes I made to your code:
$to thedocument.readyevent handler so it can be used inside the event handler.$('#datepicker')jQuery object so it didn’t have to be selected more than once.varstatement back-to-back-to-back-to…Note that if the current date is the 31st and two months ago does not have 31 days this will probably create an issue for this code. August is the only month that has 31 days and two months prior does not (June has 30 days).