huyvdit / datejs

Automatically exported from code.google.com/p/datejs
Other
0 stars 0 forks source link

firstDayOfWeek does not matter #168

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I have some problem with Date.js library. I use the russian culture settings. 
First day of week is setted by 1. And it means, that my week should be since 
Monday till Sunday. Monday should be the return vaue when i call: 
Date.getDayNumberFromName('понедельник'); //Monday, it should return 
0 and Date.getDayNumberFromName('воскресенье'); //Sunday, it should 
return 6

and, of course, it should be actual to all weeks calculations. For example, 
today (Sunday, 3 March), this code: Date.monday(), should return 25 February. 
But it isn't so. 

I've made example on my own host: http://sulla.ru/lab/

Original issue reported on code.google.com by sullaro...@gmail.com on 3 Mar 2013 at 3:25

GoogleCodeExporter commented 8 years ago
I made debug. The sdf function, which calculate shift, is so:

    var sdf = function (n) {
        return function () {
        var t = $D.today(), shift = n - t.getDay();
            if (n === 0 && $C.firstDayOfWeek === 1 && t.getDay() !== 0) {
                shift = shift + 7;
            }
            return t.addDays(shift);
        };
    };

and i've changed it to:

    var sdf = function (n) {
        return function () {
            var t = $D.today(), shift = n - t.getDay();

            if (n === 0 && $C.firstDayOfWeek === 1 && t.getDay() !== 0) {
                shift = shift + 7;
            } else if (t.getDay() === 0 && n !== 0) {
                shift = shift - 7;
            }
            return t.addDays(shift);
        };
    };

Obviously, it solves my problem.

Original comment by sullaro...@gmail.com on 4 Mar 2013 at 8:54

GoogleCodeExporter commented 8 years ago
This is incorrect. The day of the week shouldn't change - just when the week 
starts. You're misunderstanding the purpose of that value.

Sunday will always be 0, it's just that the WEEK will start on Monday if 
firstDayOfWeek is 1. That doesn't make Monday 0 indexed. Quite the opposite.

Basically this is user error/misunderstanding.

Original comment by darkcr...@gmail.com on 19 Sep 2013 at 9:11