Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 1 * new Date()?

Reading through the jQuery source code and I have come across the following line of code:

1 * new Date()

Following a Google search, I have also seen that it is included in the Google Analytics snippet.

Can anyone explain what is happening here, and the purpose of this line?
Is it to generate a random number that will not be repeated again?

like image 943
Paul Fitzgerald Avatar asked Dec 10 '22 17:12

Paul Fitzgerald


2 Answers

new Date() will return a Date() object. When coerced to a string the object returns an ISO formatted string interpretation of the date:

console.log(new Date()); // = 'Thu Mar 17 2016 09:37:12 GMT+0000 (GMT)'

When coerced to an integer this will then return the epoch timestamp of that date:

console.log(1 * new Date()); // = 1458207432249

Here's an example showing both outcomes:

var d1 = new Date();
var d2 = 1 * new Date();

console.log(d1);
console.log(d2);

Note that the coercion to an int returns the same value as Date.now(), however that method is not available in IE8 and older.

like image 103
Rory McCrossan Avatar answered Dec 25 '22 20:12

Rory McCrossan


The part of code that you refer to is:

expando = "sizzle" + 1 * new Date(),

Is it to generate a random number that will not be repeated again?

Not so much random as one that will not recur on the host (though it might if the clock is reset to an earlier date or the code is run again before the next clock tick, which might be 1 to 15 milliseconds later).

As noted in other answers, it's equivalent to +new Date() and new Date().getTime().

However…

There is another use, which is for copying dates. Prior to ECMAScript 2015, given:

new Date(value)

if value was a Date object, then its toString method was called (see ES5 §15.9.3.2, via a call to ToPrimitive, which for Date objects calls toString, see the notes at the bottom of §8.12.8), which generates an implementation dependent string. This is parsed to generate the new date. This had the quirk in some browsers where dates that genuinely had 2 digit years being converted to dates in the 20th century (e.g. a date for 21 May, 71 would become 21 May, 1971).

Using a modern Gregorian calendar for such dates isn't a good idea anyway, so likely it didn't bother too many people. This was fixed in ECMAScript 2015 so that where value is a Date, it's converted to a Number first, which returns the time value (see §20.3.2.2 #3).

Of course it's only fixed in browsers that comply with the ECMAScript 2015 specification.

Firefox still seems to live in the past. Copying a date using new Date(date) loses the milliseconds, so it likely it's still using toString and not including the milliseconds (most browsers don't). So (in Firefox up to version 43.04, apparently fixed in version 44), copying using new Date(date) in the following returns false but in ECMAScript 2015 compliant browsers (pretty much any other browser more modern than IE 8) it returns true:

var x = new Date();
var y = new Date(x);
var z = new Date(+x);

document.write('Copy using <code>new Date(date)</code> keeps milliseconds: ' + (x.getTime() == y.getTime()));
document.write('<br>Copy using<code>new Date(+date)</code> keeps milliseconds: ' + (x.getTime() == z.getTime()));
like image 37
RobG Avatar answered Dec 25 '22 19:12

RobG