Momentjs Locale - Day of Week Config for US

丶灬走出姿态 提交于 2020-01-17 05:49:07

问题


I'm using Momentjs in several places in an App I'm working on, and it is working fine. However, I am using endOf('week') and it is resulting in Saturdays. What I am expecting is Sundays.

I've been trying to find information about it, but it is not standing out anywhere how to get around it without changing code everywhere it is used. From my digging around, Moment automatically applies locales. And from looking in one of the locale files, I can see where this day of week (dow) is configured.

Example: moment/locale/en-gb.js

    week : {
        dow : 1, // Monday is the first day of the week.
        doy : 4  // The week that contains Jan 4th is the first week of the year.
    }

So what I gather is if I were in GB, end of week would be Sundays. But what I'm not clear on, is where are the defaults for U.S.? There is no en-US.js in the locale directory and it seems to default to dow:0

What I'm trying to determine is:

  1. Is there a reason there's no en-US?
  2. Is my best solution to just copy the en-gb.js as en-US.js? If I do that and include it in my App, will this affect others in other locales?

I did tinker in moment.min.js and changed dow from 0 to 1, and that resulted in Sundays being the end of week throughout the App. But that's not something I want to do.

Hopefully this is not just me missing the answers somewhere.

Any suggestions are welcome.

Thank you.

Update 1

Using:

  • momentjs 2.9.0
  • angular-moment 0.10.3

回答1:


The week system in moment is not the easiest thing to understand. That said, the default 'en' is kind of understood to be the same as 'en-us'. If you want to update day of week to be Sunday, you can use locale customization. Ensure that you are using version 2.12 or greater, as that is when the updateLocale feature was introduced. Then simply run:

moment.updateLocale('en',  {week : {
    dow : 1, // Monday is the first day of the week.
    doy : 4  // The week that contains Jan 4th is the first week of the year.
}});

This results in:

moment().endOf('week').format()
"2016-07-03T23:59:59-05:00"

If you are using 2.8 to 2.11, the syntax is simply:

moment.locale('en',  {week : {
    dow : 1, // Monday is the first day of the week.
    doy : 4  // The week that contains Jan 4th is the first week of the year.
}});

So, not terribly different, but a bit ambiguous.



来源:https://stackoverflow.com/questions/38133313/momentjs-locale-day-of-week-config-for-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!