Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 57745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:44:21+00:00 2026-05-10T17:44:21+00:00

Let me start with a specific example of what I’m trying to do. I

  • 0

Let me start with a specific example of what I’m trying to do.

I have an array of year, month, day, hour, minute, second and millisecond components in the form [ 2008, 10, 8, 00, 16, 34, 254 ]. I’d like to instantiate a Date object using the following standard constructor:

new Date(year, month, date [, hour, minute, second, millisecond ]) 

How can I pass my array to this constructor to get a new Date instance? [ Update: My question actually extends beyond this specific example. I’d like a general solution for built-in JavaScript classes like Date, Array, RegExp, etc. whose constructors are beyond my reach. ]

I’m trying to do something like the following:

var comps = [ 2008, 10, 8, 00, 16, 34, 254 ]; var d = Date.prototype.constructor.apply(this, comps); 

I probably need a ‘new‘ in there somewhere. The above just returns the current time as if I had called ‘(new Date()).toString()‘. I also acknowledge that I may be completely in the wrong direction with the above 🙂

Note: No eval() and no accessing the array items one by one, please. I’m pretty sure I should be able to use the array as is.


Update: Further Experiments

Since no one has been able to come up with a working answer yet, I’ve done more playing around. Here’s a new discovery.

I can do this with my own class:

function Foo(a, b) {     this.a = a;     this.b = b;      this.toString = function () {         return this.a + this.b;     }; }  var foo = new Foo(1, 2); Foo.prototype.constructor.apply(foo, [4, 8]); document.write(foo); // Returns 12 -- yay! 

But it doesn’t work with the intrinsic Date class:

var d = new Date(); Date.prototype.constructor.call(d, 1000); document.write(d); // Still returns current time :( 

Neither does it work with Number:

var n = new Number(42); Number.prototype.constructor.call(n, 666); document.write(n); // Returns 42 

Maybe this just isn’t possible with intrinsic objects? I’m testing with Firefox BTW.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:44:22+00:00Added an answer on May 10, 2026 at 5:44 pm

    I’ve done more investigation of my own and came up with the conclusion that this is an impossible feat, due to how the Date class is implemented.

    I’ve inspected the SpiderMonkey source code to see how Date was implemented. I think it all boils down to the following few lines:

    static JSBool Date(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {     jsdouble *date;     JSString *str;     jsdouble d;      /* Date called as function. */     if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {         int64 us, ms, us2ms;         jsdouble msec_time;          /* NSPR 2.0 docs say 'We do not support PRMJ_NowMS and PRMJ_NowS',          * so compute ms from PRMJ_Now.          */         us = PRMJ_Now();         JSLL_UI2L(us2ms, PRMJ_USEC_PER_MSEC);         JSLL_DIV(ms, us, us2ms);         JSLL_L2D(msec_time, ms);          return date_format(cx, msec_time, FORMATSPEC_FULL, rval);     }      /* Date called as constructor. */     // ... (from here on it checks the arg count to decide how to create the date) 

    When Date is used as a function (either as Date() or Date.prototype.constructor(), which are exactly the same thing), it defaults to returning the current time as a string in the locale format. This is regardless of any arguments that are passed in:

    alert(Date()); // Returns 'Thu Oct 09 2008 23:15:54 ...' alert(typeof Date()); // Returns 'string'  alert(Date(42)); // Same thing, 'Thu Oct 09 2008 23:15:54 ...' alert(Date(2008, 10, 10)); // Ditto alert(Date(null)); // Just doesn't care 

    I don’t think there’s anything that can be done at the JS level to circumvent this. And this is probably the end of my pursuit in this topic.

    I’ve also noticed something interesting:

        /* Set the value of the Date.prototype date to NaN */     proto_date = date_constructor(cx, proto);     if (!proto_date)         return NULL;     *proto_date = *cx->runtime->jsNaN; 

    Date.prototype is a Date instance with the internal value of NaN and therefore,

    alert(Date.prototype); // Always returns 'Invalid Date'                        // on Firefox, Opera, Safari, Chrome                        // but not Internet Explorer 

    IE doesn’t disappoint us. It does things a bit differently and probably sets the internal value to -1 so that Date.prototype always returns a date slightly before epoch.


    Update

    I’ve finally dug into ECMA-262 itself and it turns out, what I’m trying to achieve (with the Date object) is — by definition — not possible:

    15.9.2 The Date Constructor Called as a Function

    When Date is called as a function rather than as a constructor, it returns a string representing the current time (UTC).

    NOTE The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments.

    15.9.2.1 Date ( [ year [, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] ] ] )

    All of the arguments are optional; any arguments supplied are accepted but are completely ignored. A string is created and returned as if by the expression (new Date()).toString().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me start by saying that I do not advocate this approach, but I
Let me start off with a bit of background. This morning one of our
Let me start by saying I'm a huge fan of the elegance of this
Let's start with the following snippet: Foreach(Record item in RecordList){ .. item = UpdateRecord(item,
Let me start out by saying that I'm not a JavaScript developer so this
Let me start by explaining our set-up: I am working with some contractors. They
First off, let me start off that I am not a .net developer. The
Let's say you want to start contributing to an open source project with thousands
Before I start with the real question, let me just say that I might
Goal Let me start with my final vision of what I'd like to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.