Momentjs语言环境-美国星期几配置 [英] Momentjs Locale - Day of Week Config for US

查看:498
本文介绍了Momentjs语言环境-美国星期几配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的应用程序中的多个地方都使用Momentjs,并且运行良好.但是,我使用的是endOf('week'),它导致星期六.我期望的是星期天.

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.

我一直在寻找有关它的信息,但是在不改变使用代码的情况下,如何解决它并没有突出的意义.从我的挖掘中,Moment会自动应用语言环境.通过查看其中一个语言环境文件,我可以看到星期几(dow)的配置位置.

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.

示例:moment/locale/en-gb.js

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.
    }

所以我要收集的是,如果我使用的是GB,那么周末将是星期日.但是我不清楚,美国的默认值在哪里?语言环境目录中没有en-US.js,它似乎默认为dow:0

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

我要确定的是:

  1. 有没有没有美国的原因吗?
  2. 我最好的解决方案是仅将en-gb.js复制为en-US.js吗?如果我这样做并将其包含在我的应用程序中,这会影响其他地区的其他人吗?

我在moment.min.js中进行了修改,并将dow从0更改为1,导致整个应用程序中的星期日都结束了.但这不是我想要做的.

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.

欢迎提出任何建议.

谢谢.

更新1

使用:

  • momentjs 2.9.0
  • 角矩0.10.3

推荐答案

当前的星期系统并不是最容易理解的事情.也就是说,默认的"en"被理解为与"en-us"相同.如果要将星期几更新为星期日,则可以使用语言环境自定义.确保您使用的是2.12或更高版本,因为那是在引入updateLocale功能时开始的.然后只需运行:

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.
}});

结果是:

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

如果您使用的是2.8到2.11,则语法很简单:

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.

这篇关于Momentjs语言环境-美国星期几配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆