268

I've seen this in a few places

function fn() {
    return +new Date;
}

And I can see that it is returning a timestamp rather than a date object, but I can't find any documentation on what the plus sign is doing.

Can anyone explain?

1

7 Answers 7

324

That's the + unary operator. It's equivalent to:

function(){ return Number(new Date); }

See http://xkr.us/articles/javascript/unary-add and MDN.

15
  • 4
    But like why wouldn't you use the defined getTime method on the date object?!
    – tkone
    Commented Aug 23, 2012 at 21:36
  • 34
    Under almost no circumstances should you actually use this. I just really got burnt on this. +new Date() in addition to any kind of mathematical operations will undergo a major performance penalty.
    – Geuis
    Commented Oct 11, 2012 at 0:58
  • 13
    @BradKoch in programming brevity is most certainly not the wit of the soul. As the python community has so adequately put it "explicit is always better than implicit." What if a browser changed the automatic type conversion that's implied there through a regression? Now your code just doesn't work! .getTime() will always insure it does.
    – tkone
    Commented Oct 11, 2012 at 2:13
  • 13
    @Geuis another excellent reason that just because you can doesn't meant you should!
    – tkone
    Commented Oct 11, 2012 at 2:13
  • 16
    Sine ECMAScript 5 there is Date.now(), which is the preferred way to get the date and time in milliseconds. Commented Feb 22, 2013 at 12:18
46

JavaScript is loosely typed, so it performs type coercion/conversion in certain circumstances:

http://blog.jeremymartin.name/2008/03/understanding-loose-typing-in.html
http://www.jibbering.com/faq/faq_notes/type_convert.html

Other examples:

>>> +new Date()
1224589625406
>>> +"3"
3
>>> +true
1
>>> 3 == "3"
true
1
  • >>> 3 === "3" false Commented Dec 3, 2018 at 12:54
7

A JavaScript date can be written as a string:

Thu Sep 10 2015 12:02:54 GMT+0530 (IST)

or as a number:

1441866774938

Dates written as numbers, specifies the number of milliseconds since January 1, 1970, 00:00:00.

Coming to your question it seams that by adding '+' after assignment operator '=' , converting Date to equal number value.

same can be achieve using Number() function, like Number(new Date());

var date = +new Date(); //same as 'var date =number(new Date());'
5

Here is the specification regarding the "unary add" operator. Hope it helps...

1
  • 2
    Link-only answers are not considered valuable on SO. This is a great example of why. It's defunct.
    – isherwood
    Commented Mar 3, 2022 at 13:50
3

If you remember, when you want to find the time difference between two dates, you simply do as follows:

var d1 = new Date("2000/01/01 00:00:00"); 
var d2 = new Date("2000/01/01 00:00:01");  //one second later

var t = d2 - d1; //will be 1000 (msec) = 1 sec

typeof t; // "number"

Now if you check type of d1-0, it is also a number:

t = new Date() - 0; //numeric value of Date: number of msec's since 1 Jan 1970.
typeof t; // "number"

That + will also convert the Date to Number:

typeof (+new Date()) //"number"

But note that 0 + new Date() will not be treated similarly! It will be concatenated as string:

0 + new Date() // "0Tue Oct 16 05:03:24 PDT 2018"
2

It is a unary add operator and also used for explicit Number conversion, so when you call +new Date(), it tries to get the numeric value of that object using valueOf() like we get string from toString()

new Date().valueOf() == (+new Date)  // true
-3

It does exactly the same thing as:

function(){ return 0+new Date; }

that has the same result as:

function(){ return new Date().getTime(); }
3
  • 11
    Nope on 0+new Date. That first converts the date to a string and then prepends a "0", (eg: "0Tue Oct 21 2008 20:38:05 GMT-0400");
    – Chris Noe
    Commented Oct 22, 2008 at 0:40
  • 2
    1 * new Date will, but 1 + new Date --> String Commented Dec 3, 2008 at 15:56
  • ya, also the same goes for new Date/1
    – Ashish
    Commented Aug 26, 2015 at 9:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.