如拿到年月为字符串“2023-04”,最后得出当月30天
实现代码:
//在要用到数据的事件中写
getDatNum(){
// 获取当月天数
let year = this.searchObj.alarmTime.substring(0, 4);
// console.log("year", year);//2023
let month = this.searchObj.alarmTime.substring(5, 7);
// console.log("month1", month);//04
let monDate = this.getDaysInMonth(year, month);
// console.log("monooo", monDate);//30
var charArr = [];
for (let i = 0; i < monDate; i++) {
let obj = {};
obj.day = i + 1;
obj.value = null;
charArr.push(obj);
// console.log('charrrrr',charArr)
//最后数组[{day:1,value:null},{day:2,value:null},......]
}
},
//计算天数的事件
getDaysInMonth(year, month) {
month = parseInt(month, 10);
var temp = new Date(year, month, 0);
return temp.getDate();
},