hassanab
hassanab

Reputation: 109

jquery datepicker getdate format

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

Upvotes: 10

Views: 41942

Answers (2)

Mike
Mike

Reputation: 24383

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));

Upvotes: 27

Arun P Johny
Arun P Johny

Reputation: 388316

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)

Upvotes: 0

Related Questions