js 根据指定日期获取当月第一天和最后一天
function getfirstDateAndlastDate(dateStr,flag){
var date = new Date(dateStr);
var year = date.getFullYear();
var month = date.getMonth() + 1;
if(month > 12){
month = 1;
year++;
}
if (month < 10) {
month = '0' + month
}
var monthLastDay = new Date(year, month, 0).getDate();
var firstDate = year + '-' + month + '-' + '01';
var lastDate = year + '-' + month + '-' + monthLastDay;
if(flag=='first'){
return firstDate;
}
if(flag=='last'){
return lastDate;
}
}
getfirstDateAndlastDate('2023-01-11','first'); //结果:2023-01-01
getfirstDateAndlastDate('2023-01-21','last'); //结果:2023-01-31