2

I am facing a problem here where after assigning a date to the variable newRecurrDate when I change the value of newRecurrDate the value of date is also changed and I get value in variable time as zero. I understand that newRecurrDate is referring to date address and hence I face problems. How to copy the value so that I won't face any issue?

      getDaysFromGivenMonth : function (date, months) {
        var newRecurrDate = date;
        newRecurrDate.add(months, 'months');  // NO I18N
        var time = newRecurrDate - moment(date);
4
  • 1
    Try using var newRecurrDate=Object.assign({},date) Commented Jul 14, 2020 at 5:10
  • 2
    Does this answer your question? How to clone a Date object? Commented Jul 14, 2020 at 5:10
  • 1
    Does my answer help? Commented Jul 14, 2020 at 15:23
  • 1
    @mgm793 yus it does helped Commented Jul 19, 2020 at 7:45

2 Answers 2

1

Use moment.clone to create a copy of a moment object.

var newRecurrDate = date.clone();
Sign up to request clarification or add additional context in comments.

Comments

1

Substitute

var newRecurrDate = date;

by

var newRecurrDate = new Date(date.getTime());

This creates a own date object instead of another reference to the same date object.

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.