getCurrentMonthFirst() {
// 获取当前月第一天
let date = new Date();
date.setDate(1);
let month = parseInt(date.getMonth() + 1);
let day = date.getDate();
if (day < 10) {
day = "0" + day;
}
this.startTime = `${moment(new Date()).format("YYYY-MM")}-${day}`;
return month + "." + day;
},
getCurrentMonthLast() {
// 获取当前月最后一天
let date = new Date();
let currentMonth = date.getMonth();
let nextMonth = ++currentMonth;
let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1); // 下个月的第一天
let oneDay = 1000 * 60 * 60 * 24;
let lastTime = new Date(nextMonthFirstDay - oneDay); // 下个月的第一天减去一天,就是上个月的最后一天
let month = parseInt(lastTime.getMonth() + 1);
let day = lastTime.getDate();
if (day < 10) {
day = "0" + day;
}
this.endTime = `${moment(new Date()).format(
"YYYY"
)}-${month}-${day}`;
return month + "." + day;
},
【JS】获取当前月份第一天最后一天
最新推荐文章于 2024-05-17 16:08:24 发布