TransWikia.com

Array of dates prints unexpected dates in moment js

Stack Overflow Asked by Jayakrishnan on November 12, 2021

In my code snippet, moment js is not giving the expected result.

weekStart is set to start of the week (ie 19th Jul ) and weekEnd is set to end of the week (ie. 25th Jul).

while printing individually both are printing the correct dates, but when it is used from an array(validRange), weekStart is printing 25th Jul instead of 19th Jul.

Is there any solution for this? what could be the reason?.

 const today = moment() // let take today is 22 July 2020

const weekStart = today.startOf('week');   // 19th
console.log(weekStart.toString()); // Sun Jul 19 2020 00:00:00 GMT+0530

const weekEnd = today.endOf('week');   // 25th
console.log(weekEnd.toString()); // Sat Jul 25 2020 23:59:59 GMT+0530

const validRange = [weekStart,weekEnd]; // should be an array 
console.log(validRange.map(item =>item.toString())); // ["Sat Jul 25 2020 23:59:59 GMT+0530", "Sat Jul 25 2020 23:59:59 GMT+0530"]
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Sample output:
enter image description here

3 Answers

The methods startOf and endOf modify the current object. So you need to instanciate an object for each ones weekStart and weekEnd.

const today = moment() // let take today is 22 July 2020

const weekStart = moment(today).startOf('week');   // 19th
console.log(weekStart.format()); // Sun Jul 19 2020 00:00:00 GMT+0530

const weekEnd = moment(today).endOf('week');   // 25th
console.log(weekEnd.format()); // Sat Jul 25 2020 23:59:59 GMT+0530

const validRange = [weekStart,weekEnd]; // should be an array 

console.log(validRange.map(item =>item.format())); // ["Sat Jul 25 2020 23:59:59 GMT+0530", "Sat Jul 25 2020 23:59:59 GMT+0530"]
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Answered by 8HoLoN on November 12, 2021

moment dates are mutable (see the docs). This means that when you do the following:

const today = moment(); // today = today

const weekStart = today.startOf('week'); // today now equals the start of the week, and
                                         // you've assigned it to a new variable

What you want to do instead is:

const today = moment(); // today = today

const weekStart = today.clone().startOf('week'); // make a clone of today and then change
                                                 // it to the start of the week

Answered by Kryten on November 12, 2021

When you call .startOf or .endOf, it modifies today in place, and returns today. You need to .clone the date to get a unique instance of it.

 const today = moment() // let take today is 22 July 2020

const weekStart = today.clone().startOf('week');   // 19th
console.log(weekStart.toString()); // Sun Jul 19 2020 00:00:00 GMT+0530

const weekEnd = today.clone().endOf('week');   // 25th
console.log(weekEnd.toString()); // Sat Jul 25 2020 23:59:59 GMT+0530

const validRange = [weekStart,weekEnd]; // should be an array 
console.log(validRange.map(item =>item.toString())); // ["Sat Jul 25 2020 23:59:59 GMT+0530", "Sat Jul 25 2020 23:59:59 GMT+0530"]
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Answered by dave on November 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP