js获取当月的第一天以及最后一天
程序员文章站
2022-03-26 19:18:10
...
废话不多说直接上代码
//获取当前月份第一天
var date = new Date();
date.setDate(1);
var months = parseInt(date.getMonth() + 1);
var days = date.getDate();
if (months < 10) {
months = '0' + months
}
if (days < 10) {
days = '0' + days
}
var datas = date.getFullYear() + '-' + months + '-' + days;
console.log(datas)
// var date = new Date();
//获取当月最后一天
var currentMonth = date.getMonth();
var nextMonth = ++currentMonth;
var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
var oneDay = 1000 * 60 * 60 * 24;
var lastTime = new Date(nextMonthFirstDay - oneDay);
var month = parseInt(lastTime.getMonth() + 1);
var day = lastTime.getDate();
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
//当月最后一天
var data = date.getFullYear() + '-' + month + '-' + day;
console.log(data)