第一种:JS
获取当前月的起止日期
javascript
var now = new Date();
var nowYear = now.getFullYear(); // 当前年
let nowMonth = now.getMonth() + 1; // 当前月
var endMonthDay = new Date(nowYear, nowMonth, 0).getDate() // 当前月的总天数
var dateArr = [
nowYear + '-' + nowMonth + '-01',
nowYear + '-' + nowMonth + '-' + endMonthDay
];
console.log(11, dateArr) // ['2023-12-01', '2023-12-31']
第二种:moment
获取当前月的起止日期
下载moment插件
javascript
npm install moment
页面中引入moment
javascript
import moment from 'moment';
javascript
var startMonth = moment().startOf("month");
var endMonth = moment().endOf("month");
var dateArr = [
startMonth.format("YYYY-MM-DD"),
endMonth.format("YYYY-MM-DD")
];
console.log(11, dateArr) // ['2023-12-01', '2023-12-31']