Link to home
Start Free TrialLog in
Avatar of GTwoD
GTwoDFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Deep Copy in Java script (Date)

Hi

how do I do a deep copy in JS, specifically with the date object.

E.g.

var now = new Date();
var tomorrow = now;// this does a shalow copy

var tomorrow.setDate( tomorrow.getDate+1);// this does will add a day to both refrences to the same date object :-(

My question is how would I do a deep copy of the now object and refer by tomorrow

regards

g2d
Avatar of hernst42
hernst42
Flag of Germany image

use:

var now = new Date();
var tomorrow = new Date(now.getMilliseconds());
tomorrow.setDate( tomorrow.getDate()+1);
Avatar of Michel Plungjan
now.getMilliseconds() ??
You mean now.getTime();
No now.getMilliseconds() is correct.
The constuctor of Date also accepts miliseconds. AFAIK Date constructot has 5 different contruct parameters-sets, one is Milliseconds.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try it yourself

javascript:alert( new Date(new Date(1185449647512).getMilliseconds()))
Avatar of GTwoD

ASKER

Thanks both,
Perhaps the working version should have been the accepted solution?
Avatar of GTwoD

ASKER

sorry, mplungjan, you are right... I tohugt the othe version worked but it did not.

if you know a way I can retify this let me know.

g2d