254

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
  • 74,346
  • 29
  • 82
  • 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

312

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
  • 52,576
  • 15
  • 105
  • 143
kentaromiura
  • 6,369
  • 2
  • 20
  • 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. Look at this jsperf, http://jsperf.com/new-date-timing – 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
  • 15
    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
44

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
  • 60,358
  • 16
  • 139
  • 149
6

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

Henry
  • 494
  • 3
  • 20
Pablo Cabrera
  • 5,654
  • 4
  • 22
  • 28
6

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
  • 787
  • 9
  • 16
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
  • 52,576
  • 15
  • 105
  • 143
S.Serpooshan
  • 6,918
  • 3
  • 31
  • 58
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,101
  • 4
  • 35
  • 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
  • 30,675
  • 9
  • 53
  • 56