In comp.lang.javas cript message <r6**********@h otpop.com>, Sun, 13 Apr
2008 14:10:03, Lasse Reichstein Nielsen <lr*@hotpop.com posted:
>VK <sc**********@y ahoo.comwrites:
>Your sample doesn't have sense in the context of your question. Date
constructor expects primitives, so providing daz being type of object
just leads to invalid date result.
Actually, if d is a Date objet, then
new Date(d)
is equvialent to
new Date(d.valueOf( ))
which is again equivalent to
new Date(d.getTime( ))
which indeed creates a Date object with the same time value as the
original.
JavaScript new Date() likes to receive a string argument, and a Date
Object likes to provide a String. The equivalence is thus to
new Date(d.toString ()). Or so I thought from using IE4 IE6 IE7.
Using d.toString() provides a correct representation of the value of the
Date Object, but truncated to the Second.
Therefore, such a copy will be unfaithful to a degree which is often
unimportant. Proof : execute (I used js-quick.htm)
d1 = new Date()
d2 = new Date(d1)
x = [d1%1000, d2%1000]
I get in IE7 [Random(1000),0]; in FF2, Op9, Sf3 [Random(1000),sa me]; I
had expected that all would follow IE.
That, naturally, leads those who only test in a reputable browser to
write code which may err in a common one.
Moreover, although d.toString() only makes a small error, new Date(d)
can make a rather large one if presented with dates in some or all years
in 99BC to 99AD inclusive; it adds, or at least can add, 1900 years.
Trying :
d1 = new Date(100,0,0) // Year 99, Dec 31
d2 = new Date(d1)
x = [d1, d2]
I get d2 in 1899 in IE but d2 in 0099 in the other three.
For both tests, new Date(+d1) always gives an accurate copy.
Consider now timing :
K_ = ???
D0_ = new Date()
Q_ = K_ ; while (Q_--) { }
D1_ = new Date()
Q_ = K_ ; while (Q_--) { new Date(D0_) }
D2_ = new Date()
Q_ = K_ ; while (Q_--) { new Date(+D0_) }
D3_ = new Date()
Q_ = [D1_-D0_, D2_-D1_, D3_-D2_] // Demo 6
->
IE7 K=5e4 Result 16,953,297
FF2 K=5e4 Result 94,500,515
Op9 K=5e4 Result 31,141,125
Sf3 K=5e5 Result 157,406,562 // Note bigger K
Therefore, one should use the + if the code might be executed mainly in
IE, and should omit it if the code might be executed mainly in Safari;
but one must use the + for full range work where IE is possible.
The benefits of that + were discussed here a while ago; perhaps in the
regrettable period where we were not honoured by LRN's presence. I then
had only IE4; and there were no reports of differences with other
browsers.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demo n.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.