代碼如下:
<html>
<head>
<title>計算星期的特效-www.update8.com</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function mod(x, x_div){ //一個有用的函數(shù),得到兩數(shù)相除的余數(shù)
for (var i=x; i>=x_div; i -= x_div);
return i;
}
function checkNum(str, min, max) { //檢查輸入內(nèi)容是否正確
if (str == "") { //如果輸入為空
alert("請輸入你的選擇");
return false; //取消計算
}
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i + 1);
if (ch < "0" || ch > "9") {//如果輸入的內(nèi)容不是1~9的數(shù)字
alert("請你輸入數(shù)字");
return false;
}
}
var val = parseInt(str, 10); //將輸入內(nèi)容按十進制轉(zhuǎn)換成數(shù)值
if ((val < min) || (val > max)) { //檢查輸入數(shù)值是否超出允許范圍
alert("請注意你輸入的信息,不要超過了"+max+"。");
return false;
}
return true;//如果輸入內(nèi)容符合要求,則返回檢查結(jié)果"true"
}
function jisuan(form){
//檢查日期的正確性
if ((checkNum(form.day.value,1,31)) && (checkNum(form.month.value,1,12)) && (checkNum(form.year.value,0,99))){
var cur_day = parseInt(form.day.value,10); //將“日”部分由字符串轉(zhuǎn)換成十進制整型數(shù)
var cur_month = parseInt(form.month.value,10); //將“月”部分由字符串轉(zhuǎn)換成十進制整型數(shù)
var cur_year = parseInt(form.year.value,10); //將“年”部分由字符串轉(zhuǎn)換成十進制整型數(shù)
//定義月特征值
var sig_val;
if (cur_month == 1)
sig_val = 0;
else if (cur_month == 2)
sig_val = 3;
else if (cur_month == 3)
sig_val = 3;
else if (cur_month == 4)
sig_val = 6;
else if (cur_month == 5)
sig_val = 1;
else if (cur_month == 6)
sig_val = 4;
else if (cur_month == 7)
sig_val = 6;
else if (cur_month == 8)
sig_val = 2;
else if (cur_month == 9)
sig_val = 5;
else if (cur_month == 10)
sig_val = 0;
else if (cur_month == 11)
sig_val = 3;
else if (cur_month == 12)
sig_val = 5;
//計算“星期”數(shù)值放入val1中
var val1 = mod((cur_year + parseInt(cur_year/4) + cur_day + sig_val),7);
//根據(jù)數(shù)字格式的“星期”值顯示相應(yīng)表示星期的漢字。
if (val1 == 0)
alert("星期天");
else if (val1 == 1)
alert("星期一");
else if (val1 == 2)
alert("星期二");
else if (val1 == 3)
alert("星期三");
else if (val1 == 4)
alert("星期四");
else if (val1 == 5)
alert("星期五");
else if (val1 == 6)
alert("星期六");
return true;
}
else
return false;
}
//-->
</SCRIPT>
<FORM NAME = "What_Day">
<PRE><B>請輸入年份</B><font size=2>(0-99)</font>
<INPUT TYPE="num" name="year" onChange="if (!checkNum(this.value, 0, 99)){this.focus();this.select();} else {}" size=10 value=""style="border: 1 solid #000000">
<B>請輸入月份</B><font size=2>(1-12)</font>
<INPUT TYPE="num" name="month" onChange="if (!checkNum(this.value, 1, 12)){this.focus();this.select();} else {}" size=10 value=""style="border: 1 solid #000000">
<B>請輸入日期</B><font size=2>(1-31)</font>
<INPUT TYPE="num" name="day" onChange="if (!checkNum(this.value, 1, 31)){this.focus();this.select();} else {}" size=10 value=""style="border: 1 solid #000000">
<br>
<INPUT TYPE="button" name="Find_Out" value=" 開 始 計 算 " onclick="jisuan(this.form)" style="border: 1 solid #000000"></PRE>
</FORM>
</body>
</html>
更多信息請查看IT技術(shù)專欄
2025國考·省考課程試聽報名