If you want a different behavior just for use in your plugin, then rather than replace the Date
constructor, why don't you inherit from the Date()
object with your own constructor and just use that constructor when you want the different behavior.
So, you'd leave the Date()
constructor unchanged so everyone else would get the normal Date
behavior. And, then you'd have your own constructor (call it ValidDate()
or whatever you want) that you would use when you want the special behavior.
This is the more normal object oriented behavior. Inherit from an existing object when you want a derived/special behavior while still leaving the original object untouched for normal usage.
Also, it looks like your constructor only works if you pass it a /
separated string. If any of the other forms of legal arguments to the Date()
constructor are used such as what you use in your .clone()
method, then you will try to process it as a string. It looks to me like you should test if the constructor argument is a string and only give it special processing if it is. That way, other legal constructor arguments such as passing in the getTime()
result from another Date object will still continue to work.
Here's one way to do it that deals with the limitations/oddities of the Date()
object:
function ValidDate(a,b,c,d,e,f,g) {
var date;
if (arguments.length === 0) {
// your special behavior here
date = new Date();
}
else if (arguments.length === 1) {
if (typeof a === "string") {
// add your own constructor string parsing here
} else {
// normal default processing of single constructor argument
date = new Date(a);
}
} else {
// must be this form: new Date(year, month, day, hour, minute, second, millisecond);
// the picky Date() constructor doesn't handle undefined
// for an argument so we pass zero for any missing argument
c = c || 0;
d = d || 0;
e = e || 0;
f = f || 0;
g = g || 0;
date = new Date(a,b,c,d,e,f,g);
}
// add custom methods to this Date object
date.isValid = function() {
return !isNaN(this.getTime());
}
date.clone = function() {
return ValidDate(this.getTime());
}
return date;
}
Working example: http://jsfiddle.net/jfriend00/B25LX/