DutchEcho
DutchEcho

Reputation: 155

moment.js gives wrong date

I'm using moment.js to convert dates. 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. I also use locale NL. And when I check what week number moment gives for the date 28-06-2019 then it will return week 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

I hope someone can help with this.

Upvotes: 4

Views: 534

Answers (1)

mulsun
mulsun

Reputation: 572

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>

Upvotes: 0

Related Questions