月第一天
function getMonthFirstDay(date) {const myDate = new Date(date)const tYear = myDate.getFullYear()let tMonth:any = myDate.getMonth() + 1// 月份补0if (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() + 1const nextMonth = tMonth % 12 + 1// 下个月第一天const nextMonthFirstDay = tYear + '-' + nextMonth + '-01'// 减去一天const lastDate = new Date(nextMonthFirstDay).getTime() - 1000 * 60 * 60 * 24const tDay = new Date(lastDate).getDate()// 月份补0if (tMonth < 10) {tMonth = '0' + tMonth}return tYear + '-' + tMonth + '-' + tDay}getMonthLastDay('-02-2') //'-02-28'
如果觉得《js 获取每个月的第一天和最后一天日期》对你有帮助,请点赞、收藏,并留下你的观点哦!