这是我用于日期的脚本:
<script type="text/javascript">
var datefield=document.createElement("input")
datefield.setAttribute("type", "date")
if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n')
}
</script>
当要求未满足时,警报无效。以下是我的代码:
function validateForm() {
var x = document.forms["myForm"]["from"].value;
if (x == null || x == "") {
alert("please choose the date");
return false;
}
var y = document.forms["myForm"]["to"].value;
if (y == null || y == "") {
alert("please choose the date");
return false;
}
var date1 = new Date("x");
var date2 = new Date("y");
var diffDays = (date1 - date2);
var totalSeconds = diffDays / 1000;
var days = Math.floor(totalSeconds / 86400);
if(days < 7 )
{
alert("please be more than 7 days");
return false;
}
我在获取日期之间的范围方面存在问题。任何帮助将不胜感激。
答案 0 :(得分:1)
您将变量x
和y
作为字符串传递到Date
对象中。移除周围的"
。
var date1 = new Date(x);
var date2 = new Date(y);
答案 1 :(得分:1)
变化:
var date1 = new Date("x");
var date2 = new Date("y");
为:
var date1 = new Date(x);
var date2 = new Date(y);
另请参阅How can I tell if a browser supports <input type='date'>以更可靠地检查是否支持日期类型。