
/*

-----
dates.compare(a,b)
-----

Returns a number:

-1 if a < b
0 if a = b
1 if a > b
NaN if a or b is an illegal date



-----
dates.inRange (d,start,end)
-----

Returns a boolean or NaN:

true if d is between the start and end (start is inclusive -- end is NONINCLUSIVE)
false if d is before start or after end.
NaN if one or more of the dates are illegal.


-----
dates.convert
-----

Used by the other functions to convert their input to a date object. The input can be

a date-object : The input is returned as is.
an array: Interpreted as [year,month,day]. NOTE month is 0-11.
a number : Interpreted as number of milliseconds since 1 Jan 1970 (a timestamp)
a string : Several different formats is supported, like "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
an object: Interpreted as an object with year, month and date attributes. NOTE month is 0-11.



*/

var dates = {
    convert:function(d) {
        return (
            d.constructor === Date ? d :
            d.constructor === Array ? new Date(d[0],d[1],d[2]) :
            d.constructor === Number ? new Date(d) :
            d.constructor === String ? new Date(d) :
            typeof d === "object" ? new Date(d.year,d.month,d.date) :
            NaN
        );
    },
    compare:function(a,b) {
        return (
            isFinite(a=this.convert(a).valueOf()) &&
            isFinite(b=this.convert(b).valueOf()) ?
            (a>b)-(a<b) :
            NaN
        );
    },
    inRange:function(d,start,end) {
        return (
            isFinite(d=this.convert(d).valueOf()) &&
            isFinite(start=this.convert(start).valueOf()) &&
            isFinite(end=this.convert(end).valueOf()) ?
            start <= d && d < end :
            NaN
        );
    }
}
