I have two problems in this code using jquery datepicker (first is alert the getdate method with time but I need the date only in format(yyyy-mm-dd)) the second problem I need to get the selected date into the textbox but it gives me error
<script src="jquery-1.11.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.datepick.css">
<script type="text/javascript" src="jquery.plugin.js"></script>
<script type="text/javascript" src="jquery.datepick.js"></script>
<script src="jquery.chained.min.js"></script>
<script>
$(document).ready(function(){
$('#popupDatepicker').datepick({
dateFormat: 'yyyy-mm-dd',
onSelect: function(){
var fullDate = $('#popupDatepicker').datepick('getDate');
document.getElementByID('maach_year').value=fullDate;
alert (fullDate);
}
});
});
</script>
<form action='".$server['PHP_SELF']."' method='post'>
<table>
<tr><td>Birth Date:</td><td><input type='date' id='popupDatepicker' size='15' name='birth_day'></td>
<td>Retirement Date:</td><td><input type='text' size='15' id='maach_year' name='maach_year' ></td></tr>
<input type='submit'/><br>
</table>
</form>
I want also if I can add 65 to years that user select in the birth date, put it in the retirement textbox Thanks in Advance
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.
$("#txtDate"). datepicker({ dateFormat: 'yy-mm-dd' });
The DatePicker object has a handy-dandy built-in date formatter, and since it's a utility function, it doesn't even require you to apply a DatePicker to an element.
var date = new Date();
console.log($.datepicker.formatDate("yy-mm-dd", date));
or if you need the date of a DatePicker:
var date = $("#selector").datepicker("getDate");
console.log($.datepicker.formatDate("yy-mm-dd", date));
Because the function getDate() returns a date object, not a string. You can format the date using formatDate()
$('#maach_year').val($.datepick.formatDate('yyyy-mm-dd', fullDate));
If you want to add 65 years then
fullDate.setFullYear(fullDate.getFullYear() + 65)
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