JS獲取到本周的日期
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function makeDate() {
var date = new Date();
var month = date.getMonth();
var week = date.getDay();
var month = month + 1;
var day = date.getDate();
// week:周幾,day:幾號
var weekArr = [{week:'',day:''},{week:'',day:''},{week:'',day:''},{week:'',day:''},{week:'',day:''},{week:'',day:''},{week:'',day:''}];
// 本周內今天的前幾天的數量
var leftNum = week-1;
// 本周內今天的后幾天的數量
var rightNum = 7-week;
// 本周內今天的前幾天
for(var i=0; i<leftNum; i++){
weekArr[i].week = week-(week-i)+1;
if(i==0){
weekArr[i].day = day-week;
}else{
weekArr[i].day = day-(week-i)+1;
}
}
// 本周內今天的后幾天
for(var i=0; i<rightNum; i++){
weekArr[i+week].week = week+i+1;
weekArr[i+week].day = day+i+1;
}
// 今天
weekArr[week-1].week = week;
weekArr[week-1].day = day;
console.log(weekArr)
}
makeDate();
</script>
</body>
</html>
