Skip to main content
17-Peridot
March 21, 2022
Solved

JS Code

  • March 21, 2022
  • 1 reply
  • 1159 views

I am storing same date in three variables. And subtracting days in one variable. But all variables value getting changed. Can someone pls explain why this is happening? 

This is the code: Input & Output Datatype is datetime:

var d1 = DateInp;
var d2 = DateInp;
var d3 = DateInp;
for (var sol=1;sol<=7;sol++){
d1.setDate(d1.getDate()-1);
}
var result = d3;

 

 

AP_9587236_0-1647866500076.png

AP_9587236_1-1647866530387.pngAP_9587236_2-1647866557803.png

 

Best answer by BogdanM

Hello,

 

This happens because your three date variables are actually all references pointing to the same object. You'd need to clone the input date to get separate references for each of your variables, e.g.:

var d1 = new Date(DateInp.getTime());

1 reply

BogdanM1-VisitorAnswer
1-Visitor
March 21, 2022

Hello,

 

This happens because your three date variables are actually all references pointing to the same object. You'd need to clone the input date to get separate references for each of your variables, e.g.:

var d1 = new Date(DateInp.getTime());