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

后端 未结 3 1115
眼角桃花
眼角桃花 2021-01-30 21:35

(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 wo

3条回答
  •  温柔的废话
    2021-01-30 22:00

    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() === 'undefined') {
      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(); 
    

提交回复
热议问题