Using apply with Date constructor?

S

ShaggyMoose

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);
 
S

SAM

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(myArgs) {
myArgs = "'"+myArgs.split(',').join('\',\'')+"'";
return new Date(myArgs);
}

does that could work ? no lo sè.
 
T

Thomas 'PointedEars' Lahn

ShaggyMoose said:
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).toUpperCase() + p.substr(1);
if (typeof this[m] == "function")
{
this[m](o[p]);
}
}
};

var firstContact = new Date();

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


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top