需求:通過參數(shù)某年某月,獲取該月的所有周
// 通過傳入年、月,獲取該月有幾周以及具體日期
const getWeekData = (year, month) => {
var new_year = year; //取當(dāng)前的年份
var new_month = month++; //取下一個月的第一天,方便計算(最后一天不固定)
if (new_month < 10) {
new_month = "0" + new_month;
}
var weekDay = [
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
"星期天",
];
if (new_month > 12) {
new_month -= 12; //月份減
new_year++; //年份增
}
var first_date = new Date(new_year, new_month, 1); //取當(dāng)年當(dāng)月中的第一天-時間格式
var last_Data = new Date(
first_date.getTime() - 1000 * 60 * 60 * 24
).getDate(); //獲取當(dāng)月最后一天日期
//當(dāng)月第一天是周幾
var firstzhouji =
new Date(new_year + "/" + new_month + "/" + 1).getDay() == 0
? "星期天"
: weekDay[new Date(new_year + "/" + new_month + "/" + 1).getDay() - 1];
//當(dāng)月最后一天是周幾
var lastzhouji =
new Date(new_year + "/" + new_month + "/" + last_Data).getDay() == 0
? "星期天"
: weekDay[
new Date(new_year + "/" + new_month + "/" + last_Data).getDay() - 1
];
var firsttime = ""; //第一周有幾天
if (firstzhouji == "星期一") {
firsttime = 7;
}
if (firstzhouji == "星期二") {
firsttime = 6;
}
if (firstzhouji == "星期三") {
firsttime = 5;
}
if (firstzhouji == "星期四") {
firsttime = 4;
}
if (firstzhouji == "星期五") {
firsttime = 3;
}
if (firstzhouji == "星期六") {
firsttime = 2;
}
if (firstzhouji == "星期天") {
firsttime = 1;
}
var lasttime = ""; //最后一周有幾天
if (lastzhouji == "星期一") {
lasttime = 1;
}
if (lastzhouji == "星期二") {
lasttime = 2;
}
if (lastzhouji == "星期三") {
lasttime = 3;
}
if (lastzhouji == "星期四") {
lasttime = 4;
}
if (lastzhouji == "星期五") {
lasttime = 5;
}
if (lastzhouji == "星期六") {
lasttime = 6;
}
if (lastzhouji == "星期天") {
lasttime = 7;
}
// 前后兩周 加上 剩余周數(shù) 得出總周數(shù)
var contime = 2 + (last_Data - lasttime - firsttime) / 7;
//得出每周對應(yīng)的日期
var zhouArry = [];
for (var i = 1; i <= contime; i++) {
var strTime = "";
var lastTime = "";
if (i == 1) {
strTime = year + "-" + new_month + "-" + "01";
let aa = 1 + firsttime - 1;
if (aa < 10) {
aa = "0" + aa;
}
lastTime = year + "-" + new_month + "-" + aa;
} else if (i == contime) {
let bb = last_Data - lasttime + 1;
if (bb < 10) {
bb = "0" + bb;
}
strTime = year + "-" + new_month + "-" + bb;
lastTime =
year +
"-" +
new_month +
"-" +
(last_Data < 10 ? "0" + last_Data : last_Data);
} else {
strTime = addDate(zhouArry[zhouArry.length - 1].endTime, 1);
lastTime = addDate(zhouArry[zhouArry.length - 1].endTime, 7);
}
let chBeginTimeMonth = 0;
let chBeginTimeDate = 0;
let chEndTimeDate = 0;
if (strTime.substring(8, 9) === 0) {
// 轉(zhuǎn)換開始日期的具體日
chBeginTimeDate = strTime.substring(9, 10);
} else {
chBeginTimeDate = strTime.substring(8, 10);
}
if (strTime.substring(5, 7) > 9) {
// 轉(zhuǎn)換開始日期的具體月
chBeginTimeMonth = strTime.substring(5, 7);
} else {
chBeginTimeMonth = strTime.substring(6, 7);
}
if (lastTime.substring(8, 9) === 0) {
chEndTimeDate = lastTime.substring(9, 10);
} else {
chEndTimeDate = lastTime.substring(8, 10);
}
zhouArry.push({
weeknum: i,
beginTime: strTime,
endTime: lastTime,
period:
chBeginTimeMonth +
"月" +
chBeginTimeDate +
"日" +
" " +
"-" +
" " +
chEndTimeDate +
"日",
});
//日期增加 接受兩個參數(shù), 傳入的時間,傳入時間增加的天數(shù)
function addDate(date, days) {
if (days == undefined || days == "") {
days = 1;
}
var date = new Date(date);
date.setDate(date.getDate() + days);
var month = date.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
var day = date.getDate();
if (day < 10) {
day = "0" + day;
}
return date.getFullYear() + "-" + month + "-" + day;
}
}
return zhouArry;
};

紅框中為展示的周信息
轉(zhuǎn)載自http://www.itdecent.cn/p/f06a9252de88