<HTML><HEAD>
<script type="text/javascript">
//目前只判断了4位有效输入的年份,
//日期输出格式已处理,统一为"yyyy-mm-dd"格式.
<!--
function checkInput(fieldValue,len){ //判断输入是否都为正确数据格式
if(fieldValue.length < 0 || fieldValue.length > len)
{
alert("数据长度不对,请重新填写.年份为四位,如:2006;周次为1-2位,如1或01");
return false;
}
else
{
for(var i = 0 ; i < fieldValue.length; i++ )
{
temp = fieldValue.substr(i,1)
if(isNaN(temp))
{
alert("非法数据输入!只能输入数字,请重新输入.");
return false;
}
}
var msg = false; //处理输入0/00时出现的问题.
for(var i = 0 ; i < fieldValue.length; i++ )
{
tmp = fieldValue.substr(i,1)
if(tmp != "0") { msg = true; }
}
}
if (msg == false) {alert("非法数据输入,数据不能全部为'0',请重新输入.");return false;}
}
function calcu(){
/*==================================================================================*/
//年份特殊判断,目前只实现4为有效数字输入,有空再考虑2位有效数字输入.
//4位可计算出1000--9999年间的任意时间,2位则暂时只计算1900-2099年(暂不设计)
if(document.form1.thisYear.value.length != 4)
{
alert("年份目前只能输入四位有效数字-_-! 请重新输入.");
document.form1.thisYear.focus();
return false;
}
/*==================================================================================*/
if(document.form1.thisYear.value.charAt(0) == "0")
{
alert("年份第一位不能为'0',请重新输入.");
document.form1.thisYear.focus();
return false;
}
//-------------------------------------------------------------------------------------
//判断年份输入是否合法.
if (checkInput(document.form1.thisYear.value,4) == false) { return false; }
//判断周次输入是否合法.
if (checkInput(document.form1.thisWeek.value,2) == false) { return false; }
//-------------------------------------------------------------------------------------
var yearStart = new Date(parseInt(document.form1.thisYear.value),0,1) //设置该年1.1.
var firstDay = yearStart.getDay(); //星期
var yearEnd = new Date(parseInt(document.form1.thisYear.value),11,31) //设置该年12.31.
var endDay = yearEnd.getDay(); //星期
//-------------------------------------------------------------------------------------
//判断输入是否超过最大周次.
var maxWeek; //该年最大周次.1.1.在周一到周四,则为该年第一周,否则为上年最后一周.
if((firstDay>=1&&firstDay<=4)&&(endDay==0||endDay==4||endDay==5||endDay==6))
{
maxWeek = 53; //1.1.和12.31.都在本年,则该年有53周,否则52周
}
else
{
maxWeek = 52;
}
if(parseInt(document.form1.thisWeek.value,10) > maxWeek)
{
alert(document.form1.thisYear.value + " 年只有 " + maxWeek + " 周,请重新填写周次.");
document.form1.thisWeek.focus();
return false;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
//对1.1.所在周的前后几天特殊处理.
if(firstDay>=0&&firstDay<=4) {other = firstDay - 1;}
else {other = firstDay - 8}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
//时间调整,得出要计算周的起/始时间.
//距离当年1.1.的总天数
days = (parseInt(document.form1.thisWeek.value,10)-1)*7 - other;
//转换成Ms.......
var oneMinute = 60 * 1000;
var oneHour = oneMinute * 60;
var oneDay = oneHour * 24;
//1.1.至1/1/70的毫秒数
var dateInMs = yearStart.getTime();
//当前所选周第一天离1/1/70的毫秒数.
dateInMs += oneDay * days
//日期调整(设置1/1/70至今的毫秒数)
yearStart.setTime(dateInMs);
//当前所选周最后一天处理,同上.
var weekEnd = new Date(parseInt(document.form1.thisYear.value),0,1)
var dateInMs1 = weekEnd.getTime ();
dateInMs1 += oneDay * (days + 6);
weekEnd.setTime(dateInMs1)
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
//月和日的处理,一位变两位,如:1->01.
var month = yearStart.getMonth() + 1
if(month<10) { month = "0" + month;}
var day = yearStart.getDate();
if(day<10) { day = "0" + day;}
var month1 = weekEnd.getMonth() + 1;
if(month1<10) { month1 = "0" + month1;}
var day1 = weekEnd.getDate();
if(day1<10) { day1 = "0" + day1;}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
//赋值给text框显示.
document.form1.weekTime.value = yearStart.getFullYear() + "-" + month + "-" + day;
document.form1.weekend.value = weekEnd.getFullYear() + "-" + month1 + "-" + day1;
//-------------------------------------------------------------------------------------
}
//-->
</script>
</HEAD>
<BODY>
<form name = "form1">
<P>
<font size=2 color="blue">输入年份/周次,点"计算",程序将自动计算出该年该周次的时间段:</font><br><br>
<font size=2 color="red">年 份:<input name = "thisYear" > //填写四位格式的年份,如:2006</font><br>
<font size=2 color="red">周 次:<input name = "thisWeek" > //填写要计算的周次,如1或01</font><br>
<font size=2 color="red">时间段:<input name = "weekTime" >至<input name = "weekend" > //不需要填写,程序将自动计算</font><br><br>
<input type = "button" name = "btn" value = "计算" onclick = "calcu()">
<input type = "reset" name = "reset" value = "重置"> </P>
</form>
</BODY>
</HTML>