根据月份计算第一天和最后一天, 通过2023-12得到 2023-12-01 和 2023-12-31

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function getMonthStartEnd (paramDate) {
if (!paramDate) return
paramDate = decodeURIComponent(paramDate)
paramDate = paramDate.split('-')
const day = 1 * 24 * 60 * 60 * 1000
const year = paramDate[0]
const month = paramDate[1]
const sdate = new Date()
sdate.setFullYear(year)
sdate.setMonth(month - 1)
sdate.setDate(1)
let edate = new Date(sdate)
edate.setMonth(month)
edate = new Date(edate - day)
return {
s: formatDate(sdate, 'YYYY-MM-dd'),
e: formatDate(edate, 'YYYY-MM-dd'),
}
}
1
console.log(getMonthStartEnd('2023-12')) // {s: 2023-12-01, e: 2023-12-31}