简体   繁体   English

在 javascript 日历上,月份从星期日开始,但周从星期一开始

[英]On a javascript calendar, the month starts on Sunday, but weeks start on monday

I must modify a code in javascript that is used extensively, so I would like to not have to recreate the entire calendar code.我必须修改广泛使用的 javascript 代码,所以我不想重新创建整个日历代码。 I need the week to start on Monday, which works actually, except when the month starts on Sunday (for example March 2020).我需要一周从星期一开始,这实际上有效,除非该月从星期日开始(例如 2020 年 3 月)。 This following code (using getDay) apparently creates the number of empty cells necessary.以下代码(使用 getDay)显然创建了必要的空单元格数量。

                    for(let i=1; i<date.getDay(); i++)
                {
                    let cell = document.createElement('span');
                    cell.classList.add('cell');
                    cell.classList.add('empty');
                    this.content.appendChild(cell);
                }
I'm hoping there is a way to append this when the first day of the month is Sunday. Thanks.

This is how I corrected it.这就是我纠正它的方式。 Reminder: we are creating the appropriate number of empty cells for each month.提醒:我们正在为每个月创建适当数量的空单元格。

                var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
                var firstDayWeek = firstDay.getDay();

                if (firstDayWeek > 0){
                    for(let i=1; i<date.getDay(); i++)
                    {
                        let cell = document.createElement('span');
                        cell.classList.add('cell');
                        cell.classList.add('empty');
                        this.content.appendChild(cell);
                    }
                } 

                else {
                    for(let i=0; i<6; i++)
                    {
                        let cell = document.createElement('span');
                        cell.classList.add('cell');
                        cell.classList.add('empty');
                        this.content.appendChild(cell);
                    }                       
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM