saveeth kumar
saveeth kumar

Reputation: 21

Problem while referencing javascript variables

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);

Upvotes: 2

Views: 82

Answers (2)

ebiondi
ebiondi

Reputation: 75

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.

Upvotes: 1

Unmitigated
Unmitigated

Reputation: 89254

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

var newRecurrDate = date.clone();

Upvotes: 1

Related Questions