js 獲取昨天上週 上個月 上個季度的開始時間和結束時間
阿新 • • 發佈:2019-01-26
<html> <head> <meta charset="utf-8"> <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; } var dayMSec = 24 * 3600 * 1000; var today = new Date(); //得到今天距離本週一的天數 function getDayBetweenMonday(){ //得到今天的星期數(0-6),星期日為0 var weekday = today.getDay(); //週日 if(weekday == 0){ return 6; }else{ return weekday - 1; } } function getLastDay(){ var yestodayMSec=today.getTime() -dayMSec; var yestoday = new Date(yestodayMSec); document.getElementById("beginTime").value = yestoday.format('yyyy-MM-dd'); document.getElementById("endTime").value = yestoday.format('yyyy-MM-dd'); } function getLastWeek(){ //得到距離本週一的天數 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("beginTime").value = lastMonday.format('yyyy-MM-dd'); document.getElementById("endTime").value = lastSunday.format('yyyy-MM-dd'); } function getLastMonth(){ //得到上一個月的第一天 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("beginTime").value = lastMonthFirstDay.format('yyyy-MM-dd'); document.getElementById("endTime").value = lastMonthLastDay.format('yyyy-MM-dd'); } function getLastQuarter(){ //得到上一個季度的第一天 var lastQuarterFirstDay = new Date(today.getFullYear() , today.getMonth() - 3 , 1); //得到本月第一天 var nowMonthFirstDay = new Date(today.getFullYear() , today.getMonth(), 1); //得到上一個季度的最後一天的毫秒值 var lastQuarterLastDayMSec = nowMonthFirstDay.getTime() - 1 * dayMSec; var lastQuarterLastDay = new Date(lastQuarterLastDayMSec); document.getElementById("beginTime").value = lastQuarterFirstDay.format('yyyy-MM-dd'); document.getElementById("endTime").value = lastQuarterLastDay.format('yyyy-MM-dd'); } </script> </head> <body> <button type="button" onclick="getLastDay()">昨天</button> <button type="button" onclick="getLastWeek()">上週</button> <button type="button" onclick="getLastMonth()">上月</button> <button type="button" onclick="getLastQuarter()">上季度</button> <input id="beginTime" value=""> <input id="endTime" value=""> </body> </html>