简体   繁体   English

moment.js 给出了错误的日期

[英]moment.js gives wrong date

I'm using moment.js to convert dates.我正在使用 moment.js 来转换日期。 But for some reason, it gives me the wrong date.但由于某种原因,它给了我错误的日期。 I expect to get the date 26-08-2019 but I'm getting 02-09-2019.我希望得到 26-08-2019 的日期,但我得到的日期是 02-09-2019。 I also use locale NL.我也使用语言环境 NL。 And when I check what week number moment gives for the date 28-06-2019 then it will return week 35.当我检查日期为 28-06-2019 的周数时刻时,它将返回第 35 周。

 moment.locale('nl'); var date = moment().year('2019').week('35').day('1').format("DD-MM-YYYY"); console.log("date 1:" + date); // => date 1: 02-09-2019 console.log("week: " + moment("2019-08-26").week()); //=> week: 35
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js" integrity="sha256-AdQN98MVZs44Eq2yTwtoKufhnU+uZ7v2kXnD5vqzZVo=" crossorigin="anonymous"></script>

So I don't understand why it gives me the date for week 36. I also tried isoWeek所以我不明白为什么它给了我第 36 周的日期。我也试过 isoWeek

I hope someone can help with this.我希望有人可以帮助解决这个问题。

You need to pass in the start day of the week to get the day you want:您需要通过一周的开始日期才能获得所需的日期:

 $date=moment().year('2019').week('35').day('Monday').format("DD-MM-YYYY"); console.log("date 1:"+$date); console.log("week: "+moment("2019-08-26").week());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

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

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