Converts the date in array or number or string to a date-object - Node.js Array

Node.js examples for Array:Convert Array

Description

Converts the date in array or number or string to a date-object

Demo Code


var dates = {/*from  w  w  w  .  j a  va2s.  c  o m*/
    convert: function(d) {
        // Converts the date in d to a date-object. The input can be:
        //   a date object: returned without modification
        //  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     : Any format supported by the javascript engine, 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.
        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
        )
    }

Related Tutorials