parseDate

Returns a date object by parsing a string parameter. A variety of formats are supported

JavaScript

function parseDate(t, cfg) {
    "use strict";
    // creates Date object based on string in the following formats 
    //* Use //* to enable and /* to disable test code
    if (!arguments.length) {
        t = ["2008-05-07      ", "2008/05/07      ", "2008.05.07      ",
                         "07-05-2008      ", "07/05/2008      ", "07.05.2008      ",
                         "07-15-2008      ", "07/15/2008      ", "07.15.2008      ",
                         "17-05-2008      ", "17/05/2008      ", "17.05.2008      ",
                         "7-5-2008        ", "7/5/2008        ", "7.5.2008        ",
                         "2008-05-07 09:15", "2008/05/07 9:15 ", "07.05.2008 09a15", "07-05-2008 9a15 ",
                         "2008/05/07 21:15", "2008.05.07 21:15", "07-05-2008 09p15", "07/05/2008 9p15 ",
                         "2008.05.07 12:15", "2008-05-07 00:15", "07/05/2008 12a15", "07.05.2008 12p15",
                         "17-15-2008      ", "37/5/2008       ", "7.35.2008       ",
                         "Any bad format  ", ''];

        t.forEach(function(v, i, a) {
            a[i] = v + '  ' + parseDate(v, {
                invalid: new Date()
            }) + '  ' + parseDate(v, {
                mtag: '/'
            });
        });
        console.log(t.join('\n'));
        return t;
    } //*/
    if (!t) {
        return new Date();
    }

    cfg = cfg || {};
    t = t.match(/(?:(\d{4})([\-\/.])([0-3]?\d)\2([0-3]?\d)|([0-3]?\d)([\-\/.])([0-3]?\d)\6(\d{4}))(?:\s+([012]?\d)([:hap])([0-5]\d))?/i) || [t];
    t.forEach(function(v, i, a) {
        a[i] = v && v.match(/^\d+$/) ? parseInt(v, 10) : (v || 0);
    });

    if (t.length > 1) {
        if (cfg.mtag && cfg.mtag.indexOf(t[6]) >= 0 && t[5] < 13 && t[7] < 13) {
            t[0] = t[5];
            t[5] = t[7];
            t[7] = t[0];
        }

        if (!t[1]) {
            t[1] = t[8];
            t[2] = t[6];
            t[3] = t[7];
            t[4] = t[5];
        }

        if...