Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js first day of the week incorrect

Tags:

momentjs

I'm working with Moment.js for my first time. I tried to retrieve the date of the first day of the week, and in Europe this is normally Monday. Whatever I do, I get Sunday as result of the first day of the week.

I tried to set-up different languages (local or globally), but to no avail. I use the langs.min.js file from the moment.js github page. The language file of "en-gb" and "fr" has the line of code:

dow : 1, // Monday is the first day of the week.

So I would get the date of monday when I ask for the first day of the week right? I keep getting Sunday as output.

// Create moment object
var localLang = moment();

// Set language to french
localLang.lang('fr');

// Test language
localLang.lang(); // Output: fr

// Retrieve first day of the week and format it
var dow = localLang.startOf('week').format('dddd DD-MM-YYYY'); // Output: dimanche 14-04-2013

Dimanche is french for Sunday.. As you see, moment.js can use the language file succesfully but doesn't use the day of the week configuration

JSfiddle with moment.js and langs.js to test: JSFiddle

edit: I can get the date of Monday instead of Sunday with day(1) instead of startOf('week'). But using day(0)I still get Sunday as a result. Why isn't monday the first day of the week, as configured in the language files.

like image 719
JSS Avatar asked Apr 18 '13 10:04

JSS


People also ask

How do you add 1 day in a moment object?

To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days');

Is MomentJS being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.

Is MomentJS still used?

MomentJS is a widely used time and date formatting and calculation library.


1 Answers

For anyone who come across this question lately, moment now support lang method to set locale specific setting.

Setting first week to be Monday:

moment.lang('zh-cn', {
    week : {
        dow : 1 // Monday is the first day of the week
    }
});

var date = moment().weekday(0); // date now is the first day of the week, (i.e., Monday)
like image 87
jasonslyvia Avatar answered Oct 05 '22 23:10

jasonslyvia