<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" οnclick="getLastDay()">昨天</button>
<button type="button" οnclick="getLastWeek()">上周</button>
<button type="button" οnclick="getLastMonth()">上月</button>
<button type="button" οnclick="getLastQuarter()">上季度</button>
<input id="beginTime" value="">
<input id="endTime" value="">
</body>
</html>
js 获取昨天上周 上个月 上个季度的开始时间和结束时间
最新推荐文章于 2024-06-29 19:56:34 发布