1.完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button type="button" onclick="btnClick(1)">今天</button>
<input id="day"></input>
<br/>
<br/>
<button type="button" onclick="btnClick(2)">本周</button>
<input id="week"></input>
<br/>
<br/>
<button type="button" onclick="btnClick(3)">本月</button>
<input id="month"></input>
<br/>
<br/>
<button type="button" onclick="btnClick(4)">本季</button>
<input id="quarter"></input>
<br/>
<br/>
<button type="button" onclick="btnClick(5)">今年</button>
<input id="year"></input>
<script type="text/javascript">
// 日期格式化
Date.prototype.format = function(format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
return format;
}
function chooseDate(val) {
var params;
var now = new Date() // 当前日期
var nowDayOfWeek = now.getDay() - 1 // 获取今天是本周的第几天,通过减1将周一改为本周的第一天
var nowDay = now.getDate() // 当前日
var nowMonth = now.getMonth() // 当前月