Moment: Week Days locale problems

Created on 15 Jul 2017  ·  7Comments  ·  Source: moment/moment

Description of the Issue and Steps to Reproduce:
As in the official document,
moment.locale('zh_CN');
moment.weekdays(true); // lists weekdays Monday-Sunday in Chinese
If true, the weekdays will be returned in locale specific order. But in the case, the moment locale is changed globally.

So if I just want to change the locale locally, I use
moment.localeData('zh_CN').weekdaysShort()
It always lists weekdays from Sunday-Saturday in Chinese (NOT in locale specific order). And I could not provide 'true' value to the function in this case(Causing ERROR).

Please include the values of all variables used.

Environment:

Examples: Chrome 49 on OSX, Internet Explorer 10 on Windows 7, Node.JS 4.4.4 on Ubuntu 16.0.4

Both the browser and the OS are important to us, particularly if you have an unsual environment like an IOT application.

Other information that may be helpful:

  • The time zone setting of the machine the code is running on
  • The time and date at which the code was run
  • Other libraries in use (TypeScript, Immutable.js, etc)

If you are reporting an issue, please run the following code in the environment you are using and include the output:

console.log( (new Date()).toString())
console.log((new Date()).toLocaleString())
console.log( (new Date()).getTimezoneOffset())
console.log( navigator.userAgent)
console.log(moment.version)

Ensure your issue is isolated to moment. Issues involving third party tools will be closed unless submitted by the tool's author/maintainer.

Bug Language

Most helpful comment

An interesting information is that the weekday function is locale-aware, so

const weekdays = [0, 1, 2, 3, 4, 5, 6].map(dow => moment().locale('en-gb').weekday(dow).format('dddd'))

returns ['Sunday', 'Monday', ...] for en-us.
but ['Monday', 'Tuesday', ...] for en-gb.

All 7 comments

Hmm, I can't reproduce this. I think you're saying this:

moment.locale('en') //=> "en" (I don't have the locale set to zh)

// you're saying this is NOT in the right order
moment.localeData('zh_CN').weekdays(); //=> ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]

//but if you change the global locale, then it is
moment.locale('zh_CN'); //=> "zh-cn"
moment.weekdays() //=> ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]

I wouldn't be surprised if it worked like you said it does (the localeData is getting directly at the data, so wouldn't be surprising if it didn't do the reordering, and possibly we just don't have an API for getting the ordered weekdays for a non-globally-set locale). But...aren't those arrays above the same? I don't read or speak Chinese so don't know whether they're in locally-correct order, but unless I'm missing something (always possible when looking at unfamiliar characters) they either both are or both aren't, right?

It's the same with "en-gb" and "en-us".

If my locale is 'en-us' and i want to get the 'en-gb' weekdays, including sorting, i'll be forced to change the locale globally or do some array work to get the sorted weekdays.

moment.locale('en-us')
moment.weekdays(true) // ["Sunday", "Monday", ...

moment.locale('en-gb')
moment.weekdays(true) // ["Monday", "Tuesday", ...

But

moment.localeData('en-us').weekdays() // ["Sunday", "Monday", ...

moment.localeData('en-gb').weekdays() // ["Sunday", "Monday", ...

And there is no

moment.localeData('en-gb').weekdays(true)

Hmm, yeah, I'm calling this a bug.

An interesting information is that the weekday function is locale-aware, so

const weekdays = [0, 1, 2, 3, 4, 5, 6].map(dow => moment().locale('en-gb').weekday(dow).format('dddd'))

returns ['Sunday', 'Monday', ...] for en-us.
but ['Monday', 'Tuesday', ...] for en-gb.

I did some digging.

There is a different function that implements <Moment>.weekdays vs <Locale>.weekdays (and similarly weekdaysMin, weekdaysShort, months, monthsShort)

As mentioned in the docs, the first is designed to list the current locale's months or weekdays.
https://momentjs.com/docs/#/i18n/listing-months-weekdays/

The second is designed to get information from the locale itself.
https://momentjs.com/docs/#/i18n/locale-data/

We could locale-sort the weekdays with the same parameter.

  • So, moment.localeData('en-gb').weekdays(true) would return ['Monday', 'Tuesday', ...]).
  • I think this is reasonable because this call throws an error in current versions of Moment.

I've put up a PR - let me know what you all think.

Is there a plan to fix moment().locale('en-us').weekday(0) to return Monday and not Sunday?

@inbalg No, that is correct

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cristianstaicu picture cristianstaicu  ·  24Comments

llacroix picture llacroix  ·  186Comments

mhayes14 picture mhayes14  ·  28Comments

mau21mau picture mau21mau  ·  69Comments

elmart picture elmart  ·  42Comments