0

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 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.