最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Multiplication with date object - javascript - Stack Overflow

programmeradmin4浏览0评论

I came across this piece of code var timeStamp = 1 * new Date(); and to my surprise it returned value in milliseconds since 1970/01/01. This is equivalent to using .getTime() method!

What's happening under the hood? Does Type Conversion concept work here, which basically converts new Date() value into the milliseconds?

I came across this piece of code var timeStamp = 1 * new Date(); and to my surprise it returned value in milliseconds since 1970/01/01. This is equivalent to using .getTime() method!

What's happening under the hood? Does Type Conversion concept work here, which basically converts new Date() value into the milliseconds?

Share Improve this question asked Jun 12, 2014 at 10:38 Manaday MavaniManaday Mavani 1233 silver badges15 bronze badges 2
  • Multiplication (or any arithmetic operation) causes a Date object to be converted to a number for the sake of evaluating the expression. It could also use +new Date() or new Date() / 1, etc. – RobG Commented Jun 12, 2014 at 10:41
  • When you use multiplication, internally it call the Date's prototype valueOf function which returns the value in millisecond – Akhlesh Commented Jun 12, 2014 at 10:41
Add a ment  | 

2 Answers 2

Reset to default 7

What's happening under the hood?

The short version:

Because it's being used in a math operation, the date is converted to a number, and when you convert dates to numbers, the number you get is the milliseconds-since-the-Epoch (e.g., getTime()).

The long version:

  1. The multiplication operator calls the abstract operation ToNumber on its operands.

  2. For objects like Dates, that calls the abstract operation ToPrimitive on the object, with the "preferred type" being "number".

  3. For most types of objects (including Dates), ToPrimitive calls the abstract operation [[DefaultValue]], passing along the preferred type as the "hint".

  4. [[DefaultValue]] with hint = "number" calls valueOf on the object. (valueOf is a real method, unlike the abstract operations above.)

  5. For Date objects, valueOf returns the "time value," the value you get from getTime.


Side note: There's no reason I can think of to use var timeStamp = 1 * new Date() rather than, say, var timeStamp = +new Date(), which has the same effect. Or of course, on any modern engine (and the shim is trivial), var timeStamp = Date.now() (more on Date.now).

Numeric conversion

It is because of numeric conversion in javascript which is almost same like toString but internally it is called much more often.

Numeric conversion is performed in two main cases:

  • In functions which needs a number: for example Math.sin(obj), isNaN(obj), including arithmetic operators: +obj.
  • In parisons, like obj == 'John'. The exceptions are the string equality ===, because it doesn’t do any type conversion, and also non-strict equality when both arguments are objects, not primitives: obj1 == obj2. It is true only if both arguments reference the same object.

The explicit conversion can also be done with Number(obj).

The algorithm of numeric conversion:

  • If valueOf method exists and returns a primitive, then return it.

  • Otherwise, if toString method exists and returns a primitive, then return it.

  • Otherwise, throw an exception.

Among built-in objects, Date supports both numeric and string conversion:

alert( new Date() ) // The date in human-readable form
alert( 1*new Date() ) // Microseconds from 1 Jan 1970

Further reading

发布评论

评论列表(0)

  1. 暂无评论