var dayMSec = 24 * 3600 * 1000; var today = new Date(); 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 getDayBetweenMonday(){ //得到今天的星期数(0-6),星期日为0 var weekday = today.getDay(); //周日 if(weekday == 0){ return 6; }else{ return weekday - 1; } } function gToDay(){ var todayStamp=today.getTime(); var yestoday = new Date(todayStamp); document.getElementById("start_date").value = yestoday.format('yyyy-MM-dd'); document.getElementById("end_date").value = yestoday.format('yyyy-MM-dd'); } function gLastDay(){ var yestodayMSec=today.getTime() -dayMSec; var yestoday = new Date(yestodayMSec); document.getElementById("start_date").value = yestoday.format('yyyy-MM-dd'); document.getElementById("end_date").value = yestoday.format('yyyy-MM-dd'); } function gLastWeek(){ var weekdayBetween = getDayBetweenMonday(); var nowMondayMSec = today.getTime() - weekdayBetween * dayMSec; var lastMondayMSec = nowMondayMSec - 7 * dayMSec; var lastSundayMSec = nowMondayMSec - 1 * dayMSec; var lastMonday = new Date(lastMondayMSec); var lastSunday = new Date(lastSundayMSec); document.getElementById("start_date").value = lastMonday.format('yyyy-MM-dd'); document.getElementById("end_date").value = lastSunday.format('yyyy-MM-dd'); } function gThisWeek(){ var weekdayBetween = getDayBetweenMonday(); var nowMondayMSec = today.getTime() - weekdayBetween * dayMSec; var thisMondayMSec = nowMondayMSec; var thisSundayMSec = nowMondayMSec + 6 * dayMSec; var thisMonday = new Date(thisMondayMSec); var thisSunday = new Date(thisSundayMSec); document.getElementById("start_date").value = thisMonday.format('yyyy-MM-dd'); document.getElementById("end_date").value = thisSunday.format('yyyy-MM-dd'); } function gLastMonth(){ //得到上一个月的第一天 var lastMonthFirstDay = new Date(today.getFullYear() , today.getMonth()-1 , 1); //得到本月第一天 var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1); //得到上一个月的最后一天的毫秒值 var lastMonthLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec; var lastMonthLastDay = new Date(lastMonthLastDayMSec); document.getElementById("start_date").value = lastMonthFirstDay.format('yyyy-MM-dd'); document.getElementById("end_date").value = lastMonthLastDay.format('yyyy-MM-dd'); } function gThisMonth(){ var thisMonthFirstDay = new Date(today.getFullYear() , today.getMonth() , 1); var nextMonthFirstDay = new Date(today.getFullYear() , today.getMonth()+1, 1); var thisMonthLastDayMSec = nextMonthFirstDay.getTime() - 1 * dayMSec; var thisMonthLastDay = new Date(thisMonthLastDayMSec); document.getElementById("start_date").value = thisMonthFirstDay.format('yyyy-MM-dd'); document.getElementById("end_date").value = thisMonthLastDay.format('yyyy-MM-dd'); }