new Date() keeps reference to original

There is some sort of strange behavior going on when you make a copy of a date that comes from either a JSDataset or from a JSRecord. [edit also from utils.dateFormat)]

It appears that the copied date is actually just a reference back to the original date, leading to some very unexpected results once you start manipulating the date.

This has not always been the case, but I’m not sure when it started.

var rTest = foundset.getRecord(1);
var dOriginal = rTest.mydate;
var dCopy = new Date(dOriginal);
dCopy.setDate(dCopy.getDate() +7); //dOriginal also changes!

You can get around the issue if you double wrap the copy operation (make a new Date of a new Date).

Servoy 5.2.15

Support Case

Edit: Uploaded new sample solution. The problem also exists with dates coming out of utils.dateFormat

PersistantDateReference.servoy (4.44 KB)

I have seen this also in 6.0.8.

I worked around it because I had’nt the time to figure out, what was happening!

This is because something that comes from servoy is a java data not a native javascript date.
And rhino has a feature that if it is a java date it just wraps it so that you have that write through behavior
to make a copy:

var dCopy = new Date(dOriginal.getTime())