在不同的两周内获得同一周 - momentjs
即使在看日历时,我也在这两个日期的同一一周。我在做什么错?
moment()
.set({
year: 1982,
month: 3,
day: 21,
hour: 0,
})
.weeks()
moment()
.set({
year: 1982,
month: 3,
day: 26,
hour: 0,
})
.weeks()
两者的结果为17。
I am getting the same week for these two dates even though they are in different weeks when looking at a calendar. What am I doing wrong?
moment()
.set({
year: 1982,
month: 3,
day: 21,
hour: 0,
})
.weeks()
moment()
.set({
year: 1982,
month: 3,
day: 26,
hour: 0,
})
.weeks()
Result is 17 for both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1982年3月21日是星期日。 1982年3月26日是星期五。如果您位于美国/加拿大,或者是周日的第一天,则在同一周内。因此,您将获得正确的结果。
请参阅: https://momentjs.com/docs/docs/docs/#/customization /customization/dow-dow-dow-dow-dow-dow-dow-dow-dow-dow-dow-dow-dow- /根据“一周的第一天和一年的第一周”部分
Mar 21 1982 is a Sunday. Mar 26 1982 is Friday. They are in the same week if you are located in US/Canada or any place that has Sunday as the first day of the week. So you are getting the correct result.
See: https://momentjs.com/docs/#/customization/dow-doy/ under the section "First Day of Week and First Week of Year"
根据 Moment.JS 文档:
在 1982 年的示例中,一年的第一天是星期五,因此这意味着每个星期五周数都会增加。因此,即使 4 月 21 日和 4 月 26 日(分别是您的日期)从周日到周六处于不同的周。从技术上讲,由于 Moment.JS 用来确定一年中的星期的区域设置,它们是同一周。
Per the Moment.JS docs:
In you're example 1982, the first day of the year was a Friday, so that means every Friday the week number would increment. So even though April 21st and April 26th (which are you're dates respectively) are on different weeks when going from Sunday to Saturday. They are technically the same week due to the locale that Moment.JS is using to establish the week of the year.