简体   繁体   English

Moment.js 星期数的日期不正确

[英]Moment.js incorrect date for week number

This should return the last week of the year:这应该返回一年中的最后一周:

moment().year('2021').week(51).day('monday').format('YYYY-MM-DD');

But instead it is returning 2022-12-12 .但相反,它正在返回2022-12-12 I think there is a bug in moment.js .我认为moment.js有一个错误。

Here is codepen: https://jsfiddle.net/5402bkmp/这是codepen: https://jsfiddle.net/5402bkmp/

You need to use .isoWeek instead of .week (documented here , though it's unclear to me why).您需要使用.isoWeek而不是.week在此处记录,尽管我不清楚为什么)。

You should post your code here, not elsewhere.您应该在此处而不是其他地方发布您的代码。

 var now = moment().year('2021').week(51).day('monday').format('YYYY-MM-DD'); console.log(now.toString());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Breaking down the code, if run on Monday 27 December:分解代码,如果在 12 月 27 日星期一运行:

moment()

Creates a moment object for 27 Dec 2021为 2021 年 12 月 27 日创建一个时刻 object

  .year('2021')

Sets the year to 2021, which changes nothing because it's already set to 2021. It also handles cases like 2020-02-29 + 1 year, which becomes 2021-02-28.将年份设置为 2021,这不会改变任何内容,因为它已经设置为 2021。它还处理像 2020-02-29 + 1 年这样的情况,即变为 2021-02-28。

  .week(51)

Sets to "localised" start of week 51. The problem is, how does the user know how moment.js localises things?设置为第 51 周的“本地化”开始。问题是,用户如何知道 moment.js 是如何本地化的? For me it seems to be Mon 12 Dec 2021. That numbering seems to be based on the first week starting on the first Monday on or before 1 Jan 2021 (ie Mon 28 Dec 2020).对我来说,它似乎是 2021 年 12 月 12 日星期一。该编号似乎是基于从 2021 年 1 月 1 日或之前的第一个星期一开始的第一周(即 2020 年 12 月 28 日星期一)。

  .day('monday')

Sets the date to Monday of the same localised week, again it's hard for users to know what their "localised" week is.将日期设置为同一本地化周的星期一,用户也很难知道他们的“本地化”周是什么。 For me, it just keeps it as Monday.对我来说,它只是保持星期一。

  .format('YYYY-MM-DD')

So I think it's clear that a problem with using the week method is that neither the programmer nor user know what the result will be because they don't know what moment.js is using to localise things (possibly navigator.language ).所以我认为很明显,使用week方法的一个问题是程序员和用户都不知道结果会是什么,因为他们不知道 moment.js 使用什么来本地化事物(可能是navigator.language )。 And results can be very different to what is expected.结果可能与预期的结果大不相同。

One fix, as Sergiu suggested, is to use isoWeek so at least the result is consistent and predictable.正如 Sergiu 所建议的,一种解决方法是使用isoWeek ,这样至少结果是一致且可预测的。 ISO weeks start on Monday, with the first week being the one with the most days in the subject year. ISO 周从星期一开始,第一周是本年度中天数最多的一周。 It's also expressed as the week of the first Thursday, or the week of 4 January, they all work to give the same Monday as the start of week 1 of any particular year.它也表示为第一个星期四的那一周,或 1 月 4 日的那一周,它们都致力于将同一星期一作为任何特定年份的第 1 周的开始。 Some weeks have 52 weeks, some 53, and sometimes a couple of days near the end of the year are part the first week of the following year or last week of the previous year.有些周有 52 周,有些有 53 周,有时接近年底的几天是下一年的第一周或前一年的最后一周。

You might also like to see Get week of year in JavaScript like in PHP .您可能还希望在 JavaScript 中查看获取一年中的一周,例如在 PHP 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM