<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>获得星期</title>
<script type="text/javascript">
function cot(){
y=document.getElementById("y").value;
m=document.getElementById("m").value;
d=document.getElementById("d").value;
var mydate=new Date(y,m,d);
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var myday=mydate.getDay();
document.getElementById("w").value=weekday[myday];
//document.write("今天是:" + weekday[myday]);
}
</script>
</head>
<body>
<form>
<input type="text" id="y">year</br>
<input type="text" id="m">mouth</br>
<input type="text" id="d">day</br>
<input type="button" id="b" value="count" onclick="cot()"></br>
<input type="text" id="w">week</input>
</form>
</body>
</html>
这似乎是语言本身的问题,自己设置日期星期数会产生一天的误差,如果这样改就没有问题
if(myday==0)
document.getElementById("w").value=weekday[6];
else
document.getElementById("w").value=weekday[myday-1];
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>单击事件 </title> <script type="text/javascript"> function wwt() { a = document.getElementById("n1").value; b = document.getElementById("n2").value-1; c = document.getElementById("n3").value; var tadays = new Date(a,b,c); var weekday= ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; document.getElementById("n4").value=weekday[tadays.getDay()]; } </script> </head> <body> <form> <input type="text" id="n1" />年<br /> <input type="text" id="n2" />月<br /> <input type="text" id="n3" />日<br /> <input name="点击我" onClick="wwt()" type="button" value="查询"/><br /> <input type="button" value="星期"> <input type="text" id="n4" /> </form> </body> </html>
我运行了你的代码没问题,你输入月份时注意0表示1月,1表示2月......11表示12月,即想查2016年2月15日,就输入2016 1 15