javascript - Setting up the fist day of the week. Calendar. JS - Stack Overflow

admin2025-04-19  0

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.

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.

Share Improve this question asked Jul 8, 2014 at 5:28 Th.GlebTh.Gleb 251 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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();

Here is a working code for week starting from Monday: http://jsfiddle/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++) {
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745077733a283667.html

最新回复(0)