Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain why and how +new Date(); works as 'workaround' for Date.now() in IE8 or below

Tags:

javascript

(I'm reading the book "Professional JavaScript for Web Developers" to give a context about this question, specifically Chapter 5 on Reference Types)

I'm wondering why and how var start = +new Date(); works to get the current millisecond representation of now as a work-around to browsers (e.g.: IE8) that don't support ECMAScript 5's Date.now()?

What does the + operator do here compared to just plain old new Date() which also gets the current date and time?

like image 734
Jan Carlo Viray Avatar asked Feb 24 '12 11:02

Jan Carlo Viray


People also ask

How to get time from new Date in JavaScript?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.

How to get current Date in milliseconds in JavaScript?

var A = Date. now(); Parameters: This method does not accept any parameter. Return Values: It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

How to create a Date time object in js?

The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.


2 Answers

What happens is that you first create a new Date object and then cast it to a number.

TL;DR-version

Under the hood the runtime calls valueOf method of the Date object.

Verbose-version

return a new Date object

var d = new Date; 

use the Unary + Operator

var n = +d; 

The unary + operator calls the internal ToNumber with d.

9.3 ToNumber

Takes an input argument and if the argument type is Object (Date is) call the internal ToPrimitive with input and hint Number.

9.1 ToPrimitive

takes an input argument and an optional argument PreferredType.

if input type is Object the spec says:

Return a default value for the Object. The default value of an object is retrieved by calling the [[DefaultValue]] internal method of the object, passing the optional hint PreferredType. The behaviour of the [[DefaultValue]] internal method is defined by this specification for all native ECMAScript objects in 8.12.8.

8.12.8 [[DefaultValue]] (hint)

When the [[DefaultValue]] internal method of O is called with hint Number, the following steps are taken:

  1. Let valueOf be the result of calling the [[Get]] internal method of object O with argument "valueOf".
  2. If IsCallable(valueOf) is true then,
    1. Let val be the result of calling the [[Call]] internal method of valueOf, with O as the this value and an empty argument list.
    2. If val is a primitive value, return val.

In code this approximately translates to:

var val,     type,     valueOf = O.Get( 'valueOf' );  if ( typeof valueOf === 'function' ) {     val = valueOf.call( O );     type = typeof val;     if ( val == null || type === 'boolean' || type === 'number' || type === 'string' ) {         return val;     } } 

[[Get]]ting the internal method of O with argument "valueOf" basically means returning Date.prototype.valueOf.

15.9.5.8 Date.prototype.valueOf ( )

The valueOf function returns a Number, which is this time value.

If we now go back to 9.3 ToNumber we see that ToNumber calls itself, this time with the returned val from 8.12.8 [[DefaultValue]] (hint) as primValue. If argument type is Number it says:

The result equals the input argument (no conversion).

The End

like image 86
anddoutoi Avatar answered Oct 03 '22 08:10

anddoutoi


Date.now() function on IE:

return a number of milliseconds between midnight, January 1, 1970, and the current date and time. 

Requirements

Not supported in installed versions earlier than Internet Explorer 9. However, it is supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Also supported in Windows Store apps. 

For get current Date object on IE8, you can use this:

if (typeof Date.now !== 'function') {   Date.now = function () {        return new Date();    } } 

For get time value in a Date Object (as the number of milliseconds since midnight January 1, 1970.) on IE8, you can use this:

var currentDateTime = +new Date();  
like image 37
SBotirov Avatar answered Oct 03 '22 07:10

SBotirov