<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
//获取每月的第一天和最后一天
function getLastDay() {
//获取当前日期
let date=new Date();
//获取当前年份
let new_year = date.getFullYear();
//获取当前月份
let new_month = date.getMonth()+1;
//取当年当月中的第一天
let startTime = new Date(new_year, new_month, 1)
//获取每月最后一天的日期
let endTime=(new Date(startTime.getTime() - 1000 * 60 * 60 * 24));
//转换日期格式
startTime=switchDate(new_year,new_month,startTime);
endTime=switchDate(new_year,new_month,endTime);
let time={
startTime,
endTime
}
return time
}
//转换日期格式:YYYY-MM-dd
function switchDate(year,month,day){
return `${year}-${
month.toString().length==1?'0'+month:month}-${
day.getDate().toString().length==1?'0'+day.getDate():day.getDate()}`;
}
console.log(getLastDay());
</script>
</body>
</html>
返回结果