Javascript获取本周,本月,本季,本年,上月,上周,上季,去年,上二周,上二月的时间段
function dateChange(name){ var beginTimeObject = document.getElementById("beginTime"); var endTimeObject = document.getElementById("endTime"); var beginTime; var endTime; var now = new Date(); var month = now.getMonth(); var year = now.getFullYear(); var day = now.getDate(); var wday = now.getDay(); switch(name){ case "tweek": //本周 var day = now.getDay(); now.setDate(now.getDate() - (day -1)); beginTime = formatDate(now); now.setDate(now.getDate() + 6); endTime = formatDate(now); break; case "tmonth": //本月 now.setDate(1); beginTime = formatDate(now); var days = getDays(now); now.setDate(days); endTime = formatDate(now); break; case "tquarter": //本季 var m = now.getMonth() + 1; var q = parseInt((m + 2 ) / 3 ); //得到第几季 m = q * 3 - 2; //得到季的首月份 now.setMonth(m-1); now.setDate(1); beginTime = formatDate(now); now.setMonth(now.getMonth() + 3); now.setDate(0); endTime = formatDate(now); break; case "tyear": //本年 now.setMonth(0); now.setDate(1); beginTime = formatDate(now); now.setMonth(11); now.setDate(31); endTime = formatDate(now); break; case "today": //今天 beginTime = formatDate(now); endTime = beginTime; break; case "pweek": //上周 var day = now.getDay(); now.setDate(now.getDate() - (day -1) - 7 ); beginTime = formatDate(now); now.setDate(now.getDate() + 6); endTime = formatDate(now); break; case "pmonth": //上月 now.setDate(1); now.setMonth(now.getMonth() -1 ); beginTime = formatDate(now); var days = getDays(now); now.setDate(days); endTime = formatDate(now); break; case "pquarter": //上季 var m = now.getMonth() + 1; var q = parseInt((m + 2 ) / 3 ); //得到第几季 m = q * 3 - 2; //得到季的首月份 m = m-3 ; //上季 now.setMonth(m-1); now.setDate(1); beginTime = formatDate(now); now.setMonth(now.getMonth() + 3); now.setDate(0); endTime = formatDate(now); break; case "pyear": //去年 now.setFullYear(now.getFullYear() -1 ); now.setMonth(0); now.setDate(1); beginTime = formatDate(now); now.setMonth(11); now.setDate(31); endTime = formatDate(now); break; case "p2week": //上二周 var day = now.getDay(); now.setDate(now.getDate() - (day -1) - 7 *2); beginTime = formatDate(now); now.setDate(now.getDate() + 6 + 7); endTime = formatDate(now); break; case "p2month": //上二月 now.setDate(1); now.setMonth(now.getMonth() -1*2 ); beginTime = formatDate(now); now.setMonth(now.getMonth() + 2); now.setDate(0); endTime = formatDate(now); break; case "customize": //自定义 beginTime = ""; endTime = ""; break; } beginTimeObject.value = beginTime; endTimeObject.value = endTime; }
引自:http://hi.baidu.com/xixitie/item/b3d98eeca9d9633987d9de66