Setting up the fist day of the week. Calendar. JS

大憨熊 提交于 2019-12-25 02:25:58

问题


I used this script to create a calendar with JavaScript. I rewrote it enough, for instance, I built viewing of the previous and next month's days, viewing previous or next month, etc. But the basic algorithm remains the same as it was written in that article (for loops "// fill in the days").

Well now, when everything works pretty good, I need the first day of the week to be Monday, not Sunday. Unfortunately, I can't imagine how to change the loops in order this feature to work.

So, how do I need to change the loops to make Monday the first day of the week? Thanks.


回答1:


Here is a working code for week starting from Monday: http://jsfiddle.net/VL44m/

Two changes were made:

From cal_days_labels = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
To cal_days_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat','Sun'];

From

// this loop is for weekdays (cells)
    for (var j = 0; j <= 6; j++) { 

To

// this loop is for weekdays (cells)
    for (var j = 1; j <= 7; j++) {



回答2:


Here an enhancement of the Nikhil Talreja's code: http://codepen.io/jacknumber/pen/RWLyQW

To fix month started by Sunday and avoid list of month length, use this:

var startingDay = firstDay.getDay() == 0 ? 7 : firstDay.getDay();


来源:https://stackoverflow.com/questions/24624303/setting-up-the-fist-day-of-the-week-calendar-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!