慕勒3428872
2023-12-14 15:41:11
我正在嘗試讓這段代碼為我工作:https://codepen.io/bbarry/pen/Eopdk我希望日歷從星期一開(kāi)始。我已經(jīng)將 .js 中的 days:[] 從 Sa-So 更改為 Mo-Su。但這僅更改標(biāo)題。日期還是錯(cuò)了,12月1日仍然是星期三,但今年應(yīng)該是星期二。我很確定問(wèn)題出在 .html 中這部分附近的某個(gè)地方:<thead> <tr class="c-weeks"> {{ for (i = 0; i < 7; i++) { }} <th class="c-name"> {{: days[i] }} </th> {{ } }} </tr></thead><tbody> {{ for (j = 0; j < 6 && (j < 1 || mode === 'month'); j++) { }} <tr> {{ for (i = 0; i < 7; i++) { }} {{ if (thedate > last) { dayclass = nextmonthcss; } else if (thedate >= first) { dayclass = thismonthcss; } }} <td class="calendar-day {{: dayclass }} {{: thedate.toDateCssClass() }} {{: date.toDateCssClass() === thedate.toDateCssClass() ? 'selected':'' }} {{: daycss[i] }} js-cal-option" data-date="{{: thedate.toISOString() }}"> <div class="date">{{: thedate.getDate() }}</div> {{ thedate.setDate(thedate.getDate() + 1);}} </td> {{ } }} </tr> {{ } }}</tbody>我已經(jīng)嘗試更改循環(huán)(i = 0; i < 7; i++)但無(wú)法修復(fù)它。
1 回答

qq_花開(kāi)花謝_0
TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
html 文件中的循環(huán)for (i = 0; i < 7; i++)
僅打印 7 個(gè)標(biāo)題單元格,迭代數(shù)組days
并打印單元格中的數(shù)組值。正如您所看到的,這不會(huì)影響日期。您需要處理日期對(duì)象。
html 文件中有 2 個(gè)日期對(duì)象:一個(gè)用于月視圖,另一個(gè)用于周視圖和日視圖。
月視圖
Date 對(duì)象在 html 文件的第 12 行實(shí)例化:
first?=?new?Date(year,?month,?1),
這意味著新的日期對(duì)象,傳遞參數(shù)年(上面為當(dāng)前年份)和月份(上面定義為當(dāng)前年份)。第三個(gè)參數(shù)是被視為該月第一天的月份中的某一天。將該參數(shù)設(shè)置為0。
first?=?new?Date(year,?month,?0),
周視圖
Date 對(duì)象在 html 文件的第 20 行實(shí)例化:
thedate?=?new?Date(date);
日期設(shè)置在第 21 行:
thedate.setDate(date.getDate()?-?date.getDay());
只需設(shè)置日期如下:
thedate.setDate(date.getDate()?-?date.getDay()+1);
日視圖:無(wú)需更改,因?yàn)槟褂弥芤晥D的相同對(duì)象。
添加回答
舉報(bào)
0/150
提交
取消