cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

JS Code

AP_9587236
17-Peridot

JS Code

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

 

ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

1 REPLY 1

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


Top Tags