Newbie question on copying one date variable to another

M

mdupris

OK, so I realize JavaScript uses essentially a "by reference" style
within its assignment operator. Thus, in this script:

var dtSrce;
var dtTarget;

dtSrc = new Date();
alert("Src before: " + dtSrc);

dtTarget = dtSrc;
dtTarget.setDate(14);

alert("Src after: " + dtSrc + "\nTarget after: " + dtTarget) ;

the values of dtSrc and dtTarget end up identical. What I'm stumbling
over is how to get around this, that is, how to I create a separate,
independent date object which happens to have the same value (and yes,
presumably other properties) as the first object? Any education here
would be greatly appreciated.
 
R

rob.nikander

I don't know if there is a general solution or not. I don't see
anything like a "clone()" method on the Object class.

But in this case, since the Date can be constructed from a string or
millisecond representation, you can create a copy like this (both the
commented and uncommented copy lines seem to work).

var d = new Date();
// var copy = new Date(d.toUTCString());
var copy = new Date(d.getTime());
document.write(d.toUTCString() + "<br/>");
document.write(copy.toUTCString() + "<br/>");
d.setFullYear(2003);
document.write(d.toUTCString() + "<br/>");
document.write(copy.toUTCString() + "<br/>");

Rob
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 11 Aug 2006 15:25:57 remote, seen in
OK, so I realize JavaScript uses essentially a "by reference" style
within its assignment operator. Thus, in this script:

var dtSrce;
var dtTarget;

dtSrc = new Date();
alert("Src before: " + dtSrc);

dtTarget = dtSrc;
dtTarget.setDate(14);

alert("Src after: " + dtSrc + "\nTarget after: " + dtTarget) ;

the values of dtSrc and dtTarget end up identical. What I'm stumbling
over is how to get around this, that is, how to I create a separate,
independent date object which happens to have the same value (and yes,
presumably other properties) as the first object? Any education here
would be greatly appreciated.

Those who have properly used the newsgroup FAQ will know the answer; see
sig below.

Above, dtTarget and dtSrc refer to the same object.

Use dtTarget = new Date(+dtSrc) to get the same value in a new
Object.

Your dtTarget will then have all the properties belonging, at that
stage, to a Date Object; but it would not have any that were added to
dtSrc after its creation.

If the + is omitted, the transfer will be much slower, and for some
values in at least some browsers it will fail.

In your example, it would be better to use setYear(2000) instead of
setDate(14) since if tested on Monday the latter will have no effect.
 
M

mdupris

Rob,

Thanks for the fix, it seems to work just fine. A '.clone()'
method would be just the ticket, though. Or perhaps something like "var
copy = new d();".

= Marchand =
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 11 Aug 2006 16:22:57 remote, seen in
But in this case, since the Date can be constructed from a string or
millisecond representation, you can create a copy like this (both the
commented and uncommented copy lines seem to work).

var d = new Date();
// var copy = new Date(d.toUTCString());


Testing on a single date out of the ~2e8 available is not enough! Try

var d = new Date(100, 0, 0);
copy1 = new Date(d.toUTCString());
copy2 = new Date(d);
copy3 = new Date(+d);
alert(d + "\n" + copy1 + "\n" + copy2 + "\n" + copy3)

var copy = new Date(d.getTime());
// +d suffices
 
M

mdupris

John
Thanks for the "new Date(+dtSrc)" idiom. It's certainly a more
elegant solution and consistent with the rest of JavaScript.
You mention a workgroup FAQ. For someone searching the groups using
Google Groups for a specific topic, something like a generalized FAQ
for a workgroup is totally under the radar. You also don't mention
where I could find them. Could you pass that on the relevant link?
Sounds like there's probably a bunch of stuff there I would find
useful. Thanx!

= Marchand =
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 15 Aug 2006 08:41:58 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :

Responses should go after trimmed quotes - see newsgroup FAQ.
Thanks for the "new Date(+dtSrc)" idiom. It's certainly a more
elegant solution and consistent with the rest of JavaScript.
You mention a workgroup FAQ. For someone searching the groups using newsgroup
Google Groups for a specific topic, something like a generalized FAQ
for a workgroup is totally under the radar.

Colloquial forms are out-of-place in international media.

"Google Groups" is a misnomer. News was running long before Google,
with well-established conventions. Google would like you to think that
these newsgroups are its own property (without actually lying about it);
and as far as I can see they make little reference to established usage.
You also don't mention
where I could find them. Could you pass that on the relevant link?

Yes I did; what part of the second line of my signature (repeated) did
you not understand? Anyway, you should have been able to find it with a
Google search for something including
comp.lang.javascript newsgroup FAQ
and there are regular daily "FAQ topic" posts in the group which also
give a URL for the FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top