263

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?

Community
  • 1
  • 1
Ken
  • 77,016
  • 30
  • 84
  • 101
  • Possible duplicate of [What is the purpose of a plus symbol before a variable?](https://stackoverflow.com/questions/6682997/what-is-the-purpose-of-a-plus-symbol-before-a-variable) – Donald Duck Aug 08 '18 at 23:16

7 Answers7

320

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

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

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

isherwood
  • 58,414
  • 16
  • 114
  • 157
kentaromiura
  • 6,459
  • 2
  • 21
  • 15
  • Good stuff - "Values of type Date will be converted to their corresponding numerical value (via valueOf()), which is the number of milliseconds since the UNIX epoch." – Kon Oct 21 '08 at 14:53
  • 4
    But like why wouldn't you use the defined `getTime` method on the date object?! – tkone Aug 23 '12 at 21:36
  • @tkone Some people like it [because it is shorter](http://stackoverflow.com/a/5036460/425313). – Brad Koch Oct 10 '12 at 22:12
  • 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 Oct 11 '12 at 00: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 Oct 11 '12 at 02:13
  • 13
    @Geuis another excellent reason that just because you can doesn't meant you should! – tkone Oct 11 '12 at 02:13
  • 16
    Sine ECMAScript 5 there is `Date.now()`, which is the preferred way to get the date and time in milliseconds. – cutemachine Feb 22 '13 at 12:18
  • Another trick is to `var x = ~~y` which converts to integer even if `y` is a float value. – Reactgular Dec 28 '16 at 15:22
  • 2
    Just to be sure: `+new Date()` is completely equivalent to `Date.now()` -- yes? – Yuval A. Jan 27 '18 at 10:55
  • @YuvalA. Yes, it is completely equivalent, apart from being significantly faster than all other methods: https://jsperf.com/get-unix-time-all-versions/1 – MacroMan Jul 30 '18 at 14:35
  • Trivia: "+new Date" is used by Gmail. They don't even put parantheses after it. – MCCCS Dec 09 '18 at 13:01
  • @Geuis can you explain what performance penalty ? testing on https://jsben.ch/ttEBh showed close to 0 difference between +newDate() and new date.getTime(). But Date.now() showed 2.5% better performance – strix25 Sep 15 '22 at 22:50
  • @strix25 Thanks for replying to my 10 year old coment. =) Sadly jsperf.com closed down a long while back. Right, so basically Date.now() didn't exist back then. `+new Date` uses type coercion, where the standard `new Date().getTime()` doesn't. `+new` is shorthand, but it hides the extra computing steps going on in the background. In most cases, `+new` would be fine, but if you use it in frequently called functions it can be a significant performance hit. Better practice in general is to never use shortcuts like this and just write out your code. Better readability for the future. – Geuis Sep 16 '22 at 04:01
  • @strix25 Following up on your jsbench profile. Not sure if its yours, but the test cases are way overcomplicated. I created this one https://jsben.ch/iczTu that just tests `+new Date()`, `new Date().getTime()`, and `Date.now()`. On current gen Chrome, the 2nd is about 5% more efficient that the first, and the 3rd is 25% faster. – Geuis Sep 16 '22 at 04:19
  • @Geuis This is just 1 real world case where function would run this and much more with 6000req/s so even 2% improvement matters – strix25 Sep 16 '22 at 12:45
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
Jonny Buchanan
  • 61,926
  • 17
  • 143
  • 150
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());'
Dev
  • 817
  • 10
  • 16
6

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

Henry
  • 564
  • 3
  • 22
Pablo Cabrera
  • 5,749
  • 4
  • 23
  • 28
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"
isherwood
  • 58,414
  • 16
  • 114
  • 157
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
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
Raghavendra
  • 5,281
  • 4
  • 36
  • 51
-3

It does exactly the same thing as:

function(){ return 0+new Date; }

that has the same result as:

function(){ return new Date().getTime(); }
Sergey Ilinsky
  • 31,255
  • 9
  • 54
  • 56