Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 884
hassanab Avatar asked Jul 15 '14 12:07

hassanab


People also ask

How do I change date format in datepicker?

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.

How do I change the date format from YYYY MM DD in jQuery?

$("#txtDate"). datepicker({ dateFormat: 'yy-mm-dd' });


2 Answers

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));
like image 57
Mike Avatar answered Sep 29 '22 10:09

Mike


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)
like image 38
Arun P Johny Avatar answered Sep 29 '22 11:09

Arun P Johny