问题
I am really new to javascript and jquery. I wanted to get the value of the date selected by the user via datepicker, and use the value in my javascript. The name of my datepicker is birthDate. This is my html code for the date picker:
<guis:datePicker required="true" label="Birthdate"
bean="${accountInfo}" field="birthDate"
value="${accountInfo?.birthDate?:"none"}"
noSelection="${['null':'']}"/>
My javascript is:
<script type="text/javascript">
function isOfLegalAge() {
var currDate = new Date();
var birthDate = $("#datepicker").val();
var diff = currDate - birthDate
alert (currDate);
alert(birthDate);
if(diff >= 18){
return true;
}else {
alert("You must be atleast 18 years old to register!");
document.location.href = '/user';
return false;
}
return true;
}
</script>
The value returned by the datepicker is in this format:
Wed Jun 13 00:00:00 PHT 1990
..while the new Date is:
Wed Jun 06 2012 17:08:52 GMT+0800 (PHT)
Please help!
Thanks!
回答1:
As you have said that you got the date in dd/mm/yyyy formatTry this,
var myDate = "06/06/2012";
var currentDate = new Date();
var compareDate = currentDate.getDate() + "/" + currentDate.getMonth() + "/" + currentDate.getFullYear();
if (myDate == compareDate)
alert("Both dates are equal!");
else
alert("Dates mismatch!");
In case, if you are using jQuery also, you can do this:
var date = '06/06/2012';
var arrDate = date.split("/");
var today = new Date();
useDate = new Date(arrDate[2], arrDate[1] -1, arrDate[0]);
if (useDate == today)
alert("Both dates are equal!");
else
alert("Dates mismatch!");
Hope this helps you!
回答2:
if you have it in #datepicker id element you can use val to get value and turn it into Date like this:
new Date( $('#datepicker').val() )
this will give you back Wed Jun 20 2012 00:00:00 GMT+0100 (BST) :)
Cheers!
回答3:
try this
<form:input cssStyle="background-image:url('../images/DateCal.png');
background-repeat:no-repeat; background-position: right;"
path="requirementDate" id="datepicker1"
/>
$(document).ready(function() {
var dates= $( "#datepicker1" ).datepicker({
changeMonth: true,
changeYear: true,
yearRange: '2012:2022'
});
});
回答4:
Try this:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
来源:https://stackoverflow.com/questions/10910727/how-to-get-the-date-on-a-datepicker