-1

I was reading this answer to Hidden Features of JavaScript? and was perplexed by this behaviour:

> new Date
< Fri Feb 26 2016 21:15:43 GMT-0500 (EST)
> +new Date
< 1456539382581
< 0 + new Date + 0
"0Fri Feb 26 2016 21:17:39 GMT-0500 (EST)0"

So, the Date constructor returns an object which, when converted to a number via unary (but not via addition) returns a number representing the Unix Epoch.

How is this possible? Reading about the topic, it seems like JS has no support for operator overloading, or only a small amount if overloading non-operator functions (add, toString, whatever) counts.

JS doesn't seem to have any such function for unary + specifically (or number coersion at all, it seems), so how is this done?

Community
  • 1
  • 1
cat
  • 3,888
  • 5
  • 32
  • 61

3 Answers3

1

Unary operator + converts its operand to a number if it isn't already.

So +new Date is similar to Number(new Date).

new Date() // Fri Feb 26 2016 21:45:32 GMT-0500 (EST)

Number(new Date()) // 1456541132899

+new Date() // 1456541132899

Update:

What's special about the Date object that makes calling Number on it return the Unix time?

Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC (From MDN). So, it makes sense when we ask it to convert it to a number, it returns us milliseconds.

satish
  • 571
  • 3
  • 6
1

Automatic type conversion of Dates.

The primitive numeric value of a Date object, returned by Date.prototype.getValue() is a Unix time stamp, the number of milliseconds since 1 January, 1970 UTC and is the value of a Date object converted to a number.

When converting an object to number following a unary + operator, the javascript engine explicitly asks the object to return a number, by calling the objects internal toPrimitive method with a hint value of "number" as described in section 7.1.1 of the ES6 standard. This mechanism converts Date objects to milliseconds after a unary + sign in fairly standard fashion.

So how to explain how Date objects become converted to a string when used as the operand of a non unary plus?. It would seem that for the non-unary case, type conversion requests an object's primitive value without supplying a result type hint and . . . Date provides an exotic toPrimitive function which converts dates to string by default instead of to number. Date and Symbol are special in this regard and differ from most other objects.

Community
  • 1
  • 1
traktor
  • 17,588
  • 4
  • 32
  • 53
-1

Here is a little description about unary plus from MDN:

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already.

If the operator cannot parse a string/value, it returns NaN.

Dumbledore
  • 450
  • 1
  • 5
  • 20
  • ...and? Read your question: ***(it) returns a number representing the Unix Epoch***. – Dumbledore Feb 27 '16 at 03:03
  • neither `"Fri Feb 26 2016 21:45:32 GMT-0500 (EST)".toNumber` nor `"Fri Feb 26 2016 21:45:32 GMT-0500 (EST)".valueOf()` are `1456541132899`, though, so this is not an answer – cat Feb 27 '16 at 03:05
  • I've never heard of `toNumber` function/property. However, `valueOf()` and `Number()` functions give the same value. Check this [fiddle](https://jsfiddle.net/uf5mgnty/). – Dumbledore Feb 27 '16 at 03:19
  • Yes, your fiddle is obvious for the Date object in question, not the string object in my comment -- you will find that `"Fri Feb 26 2016 21:45:32 GMT-0500 (EST)".toNumber` is not `1456541132899` – cat Feb 27 '16 at 03:20
  • There was no string objects in the question. The string won't be considered a date unless it's parsed by the Date class. – Dumbledore Feb 27 '16 at 03:22