Momentjs Locale - Day of Week Config for US
Asked Answered
M

1

-1

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
Maclay answered 30/6, 2016 at 21:9 Comment(1)
There a reason you need to use 2.9? I can post the code for it, but the syntax from 2.12+ makes more sense.Unrepair
U
2

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.

Unrepair answered 30/6, 2016 at 21:31 Comment(3)
Thanks. That works. Is that going to override locale for other users? Or is this just changing the dow for 'en' users?Maclay
it's just changing enUnrepair
I updated to 2.12 and used moment.updateLocale and it works. Thanks.Maclay

© 2022 - 2024 — McMap. All rights reserved.