87,994
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
function $(id){ return document.getElementById(id);}
/*-----------------------日期管理类----------------------*/
var DateManager = {
/*******************************/
/*CSDN.NavyMK
/*费了些心思,留个名吧。
/*******************************/
addDay : function(daycount, oldtime){ //添加天数方法,也可在指定日期类上添加
var dayM = 1000 * 60 * 60 * 24;
var _orignalTime = new Date(0);
if(oldtime) return new Date(oldtime - _orignalTime + dayM * daycount);
else return new Date(new Date() - _orignalTime + dayM * daycount);
}, setEndSeconds : function(now){
now.setHours(23);
now.setMinutes(59);
now.setSeconds(59);
return now;
}, getLastMonthDay : function(now){
if(!now) now = new Date();
var month = now.getMonth() + 1;
return new Date(now.getFullYear() + parseInt(month / 12), month % 12, 0, 23, 59, 59, 0);
}, getLastWeekDay : function(now, offset){ //返回周末一天。js中周日为最后一天,offset为修正值。如要取周六可设置offset = -1.如果周跨月份,返回月末日。
if(!now) now = new Date();
if(!offset) offset = 0;
var customDate = DateManager.addDay(7 - now.getDay() + offset, now);
customDate = customDate.getMonth() != now.getMonth() ? DateManager.getLastMonthDay(now) : customDate;
return DateManager.setEndSeconds(customDate);
}, getLastYearDay : function(now){ //@#$%^...
if(!now) now = new Date();
return new Date(now.getFullYear(), 11, 31, 23, 59, 59, 0);
}, dateFormat : function(date){ //简单格式化时间,方便调试
try{
var dateSplit = arguments[1] ? arguments[1] : "-";
var timeSplit = arguments[2] ? arguments[2] : ":";
var Split = arguments[2] ? arguments[2] : " ";
return [
[date.getFullYear(), date.getMonth() + 1, date.getDate()].join(dateSplit),
[date.getHours(), date.getMinutes(), date.getSeconds()].join(timeSplit)
].join(Split);
} catch(err) {
return DateManager.dateFormat(new Date(0)); //如果date不是时间对象,返回初始值
}
}
}
/*-----------------------具体处理部分---------------------------*/
//返回值
function choose(type, now){
if(!now) now = new Date();
switch(type){
case 1:
return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.setEndSeconds(now))];
case 2:
return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.getLastWeekDay(now, -1))]; //周6
case 3:
return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.getLastMonthDay(now))]; //月末
case 4:
return [DateManager.dateFormat(now), DateManager.dateFormat(DateManager.getLastYearDay(now))]; //年末
default:
return false;
}
}
function setValue(idx, now){
if(!now) now = new Date();
var vals = choose(idx, now);
if(vals){
$("v1").value = vals[0];
var v2 = $("v2");
v2.value = vals[1];
v2.disabled = idx > 1;
} else {
$("v1").value = "";
$("v2").value = "";
}
}
//如果客户端修改了v1的值,则根据值日期自动取得相关数据。该例没有日期格式化程序,注意日期和时间格式与预设统一
function whenCustomChange(){
var sel = $("sel");
var date = $("v1").value.split(" ")[0].split("-");
var time = $("v1").value.split(" ")[1].split(":");
var customDate = new Date(date[0], parseInt(date[1]) - 1, date[2], time[0], time[1], time[2]);
setValue($("sel").selectedIndex, customDate);
}
</script>
</head>
<body>
<select name="sel" id="sel" onchange="setValue(this.selectedIndex)">
<option>--指定时间--</option>
<option>每日统计</option>
<option>每周统计</option>
<option>每月统计</option>
<option>每年统计</option>
</select>
<input name="v1" type="text" id="v1" size="20" onchange="whenCustomChange()" />
-
<input name="v2" type="text" id="v2" size="20" />
</body>
</html>