月第一天
function getMonthFirstDay(date) {
const myDate = new Date(date)
const tYear = myDate.getFullYear()
let tMonth:any = myDate.getMonth() + 1
// 月份补0
if (tMonth < 10) {
tMonth = '0' + tMonth
}
return tYear + '-' + tMonth + '-01'
}
月最后一天
取所求月份的下一个月第一天,减去一天即为所求月份最后一天
function getMonthLastDay(date) {
const myDate = new Date(date)
const tYear = myDate.getFullYear()
let tMonth = myDate.getMonth() + 1
const nextMonth = tMonth % 12 + 1
// 下个月第一天
const nextMonthFirstDay = tYear + '-' + nextMonth + '-01'
// 减去一天
const lastDate = new Date(nextMonthFirstDay).getTime() - 1000 * 60 * 60 * 24
const tDay = new Date(lastDate).getDate()
// 月份补0
if (tMonth < 10) {
tMonth = '0' + tMonth
}
return tYear + '-' + tMonth + '-' + tDay
}
getMonthLastDay('2021-02-2') //'2021-02-28'