0

How we can set a our manual date not a current date of calender (e.g now date is 2021 but i want to set date of year 2020) into datepicker JQuery??

3 Answers 3

0

Simply use setYear Method of Date Object :

var NewDate = new Date().setYear(2020);
new Date(NewDate) // Your required Date
// Wed Mar 18 2020 16:58:23
2
  • Its not working it get the date 2020 well,, but i want to set a date which is getting from input value that input value date i want to set into a dapicker date and also set the restriction on month and year that just one previous month can be selected otherwise other previous month or year not be selected... ?? Commented Mar 18, 2021 at 12:00
  • <label class="control-label">Date:</label> <input type="text" class="form-control" placeholder="Select Date" name="valueMonths" id="valueMonths" readonly="readonly" value="<s:property value="#session.sessionBean.valueDate.toString().split('-')[0]"/>-<s:property value="#session.sessionBean.valueDate.toString().split('-')[1]"/>" /> This is my input here i am getting date from database and that date i want to set into date picker date and selection start from that date other previous year not selected. input in the split[0] i will get year 2021 and split[1] i will get month 03. Commented Mar 18, 2021 at 12:04
0

something like

<link href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<input type="text" id="mydate" />

<script>
 var d = new Date();
  d.setFullYear(2020);
$("#mydate").datepicker().datepicker("setDate", d);

</script>

0
var randomDay= new Date('01-01-2020');
var mm = String(randomDay.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = String(randomDay.getFullYear());
var day = String(randomDay.getDay());
var dateSet=yyyy+'-'+mm+'-'+day;
console.log(dateSet);

Set this 'dateSet' variable in your datepicker 'setDate' like this:

var date=e.target.value;
var yearMonthDay=[]; 
yearMonthDay=date.split('-');
var year = yearMonthDay[0];
var month = yearMonthDay[1]; 
var day = yearMonthDay[2];
$(this).datepicker('setDate',new Date(yearMonthDay[0],yearMonthDay[1],yearMonthDay[2]));

Not the answer you're looking for? Browse other questions tagged or ask your own question.