I have a date of birth and need to auto calculate retirement date. It works fine as long as I do not change the dateFormat.
I have been trying to change the dateformat to 'dd/mm/yy' and while calculating the retirement date, I get NaN/NaN/NaN as output for the dates selected above 13 and It works if dates selected is less than 13.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Retirement Date</title>
<link href="jquery-ui.css" rel="stylesheet">
</head>
<body>
<p>DOB: <input type="text" id="dob"></p>
<p>DORetirement: <input type="text" id="doret"></p>
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
<script>
$( function() {
$('#dob').datepicker({
onSelect: function(value) {
var retirementDate = new Date(value);
var lastDay = new Date(retirementDate.getFullYear()+58, retirementDate.getMonth() + 1, 0);
var lastDayWithSlashes = (lastDay.getMonth() + 1) + '/' + (lastDay.getDate()) + '/' + lastDay.getFullYear();
$('#doret').val(lastDayWithSlashes);
},
yearRange: "-100:+0", // last hundred years
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true
});
} );
</script>
</body>
</html>
Dateobject constructor will only work when provided with a date string inYYYY-MM-DDorMM-DD-YYYYformat. To solve your issue you'll need to store two dates - one to do the calculation in one of the above formats, and another one to show in the UI in theDD-MM-YYYYformat