Date.prototype.format = function (fmt) {
const o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
function previousMonthFirst() {
// 上月第一天
let currentDate = new Date();
currentDate.setMonth(currentDate.getMonth() - 1)
const y = currentDate.getFullYear(); //获取年份
let m = currentDate.getMonth() + 1; //获取月份
m = m < 10 ? "0" + m : m; //月份补
return [y, m, '01'].join("-");
}
function previousMonthLast() {
//上月最后一天 获取本月第一天减一天
const currentDate = new Date();
const y = currentDate.getFullYear();
let m = currentDate.getMonth() + 1;
m = m < 10 ? "0" + m : m; //月份补 0
const currentDate2 = new Date([y, m, '01'].join("-"))
currentDate2.setDate(currentDate2.getDate() - 1)
return currentDate2.format("yyyy-MM-dd")
}
function currentMonthFirst() {
//当前月第一天
const currentDate = new Date(); //获取年份
const y = currentDate.getFullYear();
let m = currentDate.getMonth() + 1;
m = m < 10 ? "0" + m : m; //月份补 0
return [y, m, "01"].join("-");
}
function currentMonthLast() {
//当前月最后一天 获取下一个月第一天减一天
const currentDate = new Date()
currentDate.setMonth(currentDate.getMonth() + 1)
let y = currentDate.getFullYear()
let m = currentDate.getMonth() + 1
m = m < 10 ? "0" + m : m
const monthLast = new Date([y, m, '01'].join('-'));
monthLast.setDate(monthLast.getDate() - 1)
return monthLast.format("yyyy-MM-dd")
}
function nextMonthFirst() {
//下个月第一天
const nextFirst = new Date()
nextFirst.setMonth(nextFirst.getMonth() + 1) //增加一个月
const nextOne2 = new Date([nextFirst.getFullYear(), nextFirst.getMonth() + 1, '01'].join('-'));
return nextOne2.format("yyyy-MM-dd")
}
function nextMonthLast() {
// 下个月最后一天 下两个月第一天 减一天
const nextLast = new Date()
nextLast.setMonth(nextLast.getMonth() + 2) //增加两个月
let y = nextLast.getFullYear()
let m = nextLast.getMonth() + 1
m = m < 10 ? "0" + m : m
const nextLast2 = new Date([y, m, '01'].join('-'));
nextLast2.setDate(nextLast2.getDate() - 1)
return nextLast2.format("yyyy-MM-dd")
}
const now = new Date();
const nowTime = now.getTime();
const day = now.getDay();
const oneDayTime = 24 * 60 * 60 * 1000;
//本周的周一
let BMondayTime = nowTime - (day - 1) * oneDayTime;
//本周的周日
let BSundayTime = nowTime + (7 - day) * oneDayTime;
BMondayTime = new Date(BMondayTime).format("yyyy-MM-dd");
BSundayTime = new Date(BSundayTime).format("yyyy-MM-dd");
//上周的周一
let SMondayTime = nowTime - (day + 6) * oneDayTime;
//上周的周日
let SSundayTime = nowTime + (0 - day) * oneDayTime;
SMondayTime = new Date(SMondayTime).format("yyyy-MM-dd");
SSundayTime = new Date(SSundayTime).format("yyyy-MM-dd");
//下周的周一
let AMondayTime = nowTime - (day - 8) * oneDayTime;
//下周的周日
let ASundayTime = nowTime + (8 + day) * oneDayTime;
AMondayTime = new Date(AMondayTime).format("yyyy-MM-dd");
ASundayTime = new Date(ASundayTime).format("yyyy-MM-dd");
因篇幅问题不能全部显示,请点此查看更多更全内容