Using apply with Date constructor?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ShaggyMoose

    Using apply with Date constructor?

    I want to apply an array of arguments against the Date constructor to
    set a specific date/time. I can't seem to find the syntax to do this.
    Using eval to expand the array into seperate arguments for "new" feels
    dirty. Is this possible?

    eg. Date.apply(?, myArgs);
  • SAM

    #2
    Re: Using apply with Date constructor?

    ShaggyMoose a écrit :
    I want to apply an array of arguments against the Date constructor to
    set a specific date/time. I can't seem to find the syntax to do this.
    Using eval to expand the array into seperate arguments for "new" feels
    dirty. Is this possible?
    >
    eg. Date.apply(?, myArgs);
    function dateConstruct(m yArgs) {
    myArgs = "'"+myArgs.spli t(',').join('\' ,\'')+"'";
    return new Date(myArgs);
    }

    does that could work ? no lo sè.

    --
    sm

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Using apply with Date constructor?

      ShaggyMoose wrote:
      I want to apply an array of arguments against the Date constructor to
      set a specific date/time. I can't seem to find the syntax to do this.
      Using eval to expand the array into seperate arguments for "new" feels
      dirty. Is this possible?
      >
      eg. Date.apply(?, myArgs);
      It is possible, like

      Date.apply(null , myArgs);

      but the outcome is not what you want. Date() when not called as a
      constructor returns a string representing the current date and time,
      no matter the arguments.

      What you can do is something like the following:

      Date.prototype. setDateTime = function(o)
      {
      for (var p in o)
      {
      var m = "set" + p.charAt(0).toU pperCase() + p.substr(1);
      if (typeof this[m] == "function")
      {
      this[m](o[p]);
      }
      }
      };

      var firstContact = new Date();

      firstContact.se tDateTime({
      year: 2063, month: 4, date: 5, hours: 11, minutes: 0, seconds: 0,
      milliseconds: 0
      });


      PointedEars

      Comment

      Working...