Passing an array to the Javascript Date constructor, is it standard?

前端 未结 4 900
轻奢々
轻奢々 2020-12-18 22:21

This works in Chrome:

var dateArray = [2012, 6, 5];
var dateObject = new Date(dateArray);

And I get June 5, 2012. I also tried on an Androi

4条回答
  •  庸人自扰
    2020-12-18 22:58

    Based on Jure's answer. I have cleared up Sergio's question in the comments by showing that the null is, in fact, the scope of the call.

    function newInstance(clazz, arguments, scope) {
      return new (Function.prototype.bind.apply(clazz, [scope].concat(arguments)));
    }
    
    // Scope is not needed here.
    var date = newInstance(Date, [2003, 0, 2, 4, 5, 6]);
    
    // 1/2/2003, 4:05:06 AM (Locale = US EST)
    document.body.innerHTML = date.toLocaleString();

提交回复
热议问题