简体   繁体   English

如何获得一年中一周的第一天?

[英]How to get the first day of week with year?

// result is: 2014/12/29
moment('2016/01/01').isoWeek(1).startOf('isoWeek').format("YYYY/MM/DD")

// result is: 2016/01/04
moment().year(2016).isoWeek(1).startOf('isoWeek').format("YYYY/MM/DD")

The second is right, but I don't know why the first go wrong?第二个是对的,但我不知道为什么第一个出错?

ISO week does not always start on January 1st. ISO 周并不总是从 1 月 1 日开始。 It might start few days before of after.它可能会在几天前或之后开始。 https://en.wikipedia.org/wiki/ISO_week_date#First_week https://en.wikipedia.org/wiki/ISO_week_date#First_week

The first ISO week of 2016 starts in 2016/01/04, so 2016/01/01 is in 2015 by the ISO weeks count, and moment('2016/01/01').isoWeek(1) gives the first week of 2015, that actually start few days before 2015, in 2014/12/29. 2016 年的第一个 ISO 周从 2016/01/04 开始,因此按 ISO 周数计算,2016/01/01 是 2015 年,而moment('2016/01/01').isoWeek(1)给出了第一周2015 年,实际上在 2015 年的前几天,即 2014/12/29 开始。

For the question in the title - the second statement with .year(2016) indeed works so you may use it.对于标题中的问题 - 带有.year(2016)的第二个语句确实有效,因此您可以使用它。

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

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