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);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80

2 Answers2

1

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

var newRecurrDate = date.clone();
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
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.

ebiondi
  • 23
  • 9