Th.Gleb
Th.Gleb

Reputation: 25

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

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.

Upvotes: 0

Views: 4711

Answers (2)

Jack NUMBER
Jack NUMBER

Reputation: 446

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

Upvotes: 5

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

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++) {

Upvotes: 1

Related Questions