js 转化为标准日期型-CarlZeng

function NLDate_parseString(sDate, bDoAlert) { var m=0; var d=0; var y=0; var val = sDate; var fmterr = “”; var year=””; var year_char_index, month_char_index, day_char_index; var rtnDate = nu…

function NLDate_parseString(sDate, bDoAlert)
{
var m=0;
var d=0;
var y=0;
var val = sDate;
var fmterr = “”;
var year=””;
var year_char_index, month_char_index, day_char_index;
var rtnDate = null;

if(!window.dateformat)
    window.dateformat = "MM/DD/YYYY";   

if(sDate == "")
{
    return new Date();
}
else if(window.dateformat == "MM/DD/YYYY")
{
    if (val.indexOf("/") != -1)
    {
        var c = val.split("/");
        if(onlydigits(c\[0\])) m = parseInt(c\[0\],10);
        if(onlydigits(c\[1\])) d = parseInt(c\[1\],10);

        if ( d > 1970 )
        {
            year = y = d;
            d = 1;
        }
        else
        {
            if(onlydigits(c\[2\])) y = parseInt(c\[2\],10);
            year=c\[2\];
        }
    }
    else
    {
        var l = val.length, str;
        str = val.substr(0,2-l%2); if(onlydigits(str)) m = parseInt(str,10);
        str = val.substr(2-l%2,2); if(onlydigits(str)) d = parseInt(str,10);
        str = val.substr(4-l%2);   if(onlydigits(str)) y = parseInt(str,10);
        year=str;
    }
}
else if(window.dateformat == "DD/MM/YYYY")
{
    if (val.indexOf("/") != -1)
    {
        var c = val.split("/");
        if(onlydigits(c\[0\])) d = parseInt(c\[0\],10);
        if(onlydigits(c\[1\])) m = parseInt(c\[1\],10);
        if(onlydigits(c\[2\])) y = parseInt(c\[2\],10);
        year=c\[2\];
    }
    else
    {
        var l = val.length, str;
        str = val.substr(0,2-l%2); if(onlydigits(str)) d = parseInt(str,10);
        str = val.substr(2-l%2,2); if(onlydigits(str)) m = parseInt(str,10);
        str = val.substr(4-l%2);   if(onlydigits(str)) y = parseInt(str,10);
        year=str;
    }
}
else if(window.dateformat == "YYYY/MM/DD")
{
    if (val.indexOf("/") != -1)
    {
        var c = val.split("/");
        if(onlydigits(c\[0\])) y = parseInt(c\[0\],10);
        if(onlydigits(c\[1\])) m = parseInt(c\[1\],10);
        if(onlydigits(c\[2\])) d = parseInt(c\[2\],10);
        year=c\[0\];
    }
    else
    {
        var l = val.length, str;
        str = val.substr(0,2-l%2); if(onlydigits(str)) y = parseInt(str,10);
        str = val.substr(2-l%2,2); if(onlydigits(str)) m = parseInt(str,10);
        str = val.substr(4-l%2);   if(onlydigits(str)) d = parseInt(str,10);
        year=str;
    }
}
else if(window.dateformat == "DD.MM.YYYY")
{
    if (val.indexOf(".") != -1)
    {
        var c = val.split(".");
        if(onlydigits(c\[0\])) d = parseInt(c\[0\],10);
        if(onlydigits(c\[1\])) m = parseInt(c\[1\],10);
        if(onlydigits(c\[2\])) y = parseInt(c\[2\],10);
        year=c\[2\];
    }
    else
    {
        var l = val.length, str;
        str = val.substr(0,2-l%2); if(onlydigits(str)) d = parseInt(str,10);
        str = val.substr(2-l%2,2); if(onlydigits(str)) m = parseInt(str,10);
        str = val.substr(4-l%2);   if(onlydigits(str)) y = parseInt(str,10);
        year=parseInt(str,10);
    }
}
else if(window.dateformat == "DD-Mon-YYYY")
{
    if (val.indexOf("-") != -1)
    {
        var c = val.split("-");
        if(onlydigits(c\[0\])) d = parseInt(c\[0\],10);
        m = getMonthIndex(c\[1\]);
        <!--alert('month index....'+m+','+c\[1\]);-->
        if(onlydigits(c\[2\])) y = parseInt(c\[2\],10);
        year=c\[2\];
    }
    else
    {
        var l = val.length, str;
        str = val.substr(0,1+l%2); if(onlydigits(str)) d = parseInt(str,10);
        str = val.substr(1+l%2,3);
        m = getMonthIndex(str);
        str = val.substr(4+l%2);   if(onlydigits(str)) y = parseInt(str,10);
        year=str;
    }
}
else if(window.dateformat == "DD-MONTH-YYYY")
{
    var comps = val.split("-");
    if(onlydigits(comps\[0\]))
        d = parseInt(comps\[0\]);
    m = arrayIndexOf(NLDate\_months, comps\[1\], true) + 1;
    if(onlydigits(comps\[2\]))
    {
        y = parseInt(comps\[2\]);
        year = y;
    }
}
else if(window.dateformat == "YYYY-MM-DD")
{
    var comps = val.split("-");
    if(onlydigits(comps\[2\]))
        d = parseInt(comps\[2\]);
    if(onlydigits(comps\[1\]))
        m = parseInt(comps\[1\]);
    if(onlydigits(comps\[0\]))
    {
        y = parseInt(comps\[0\]);
        year = y;
    }
}
    else if(window.dateformat == "EEYY年MM月DD日")
{
    year\_char\_index = val.indexOf(year\_char\_cn);
    month\_char\_index = val.indexOf(month\_char\_cn);
    day\_char\_index = val.indexOf(day\_char\_cn);
    if(onlydigits(val.substring(month\_char\_index+1,day\_char\_index)))
        d = parseInt(val.substring(month\_char\_index+1,day\_char\_index));
    if(onlydigits(val.substring(year\_char\_index+1,month\_char\_index)))
        m = parseInt(val.substring(year\_char\_index+1,month\_char\_index));
    var era = val.substring(0, 2);
    if(onlydigits(val.substring(2,year\_char\_index)))
    {
        y = get\_gregorian\_year(parseInt(val.substring(2,year\_char\_index)), era);
        year = y;
    }
}
    else if(window.dateformat == "YYYY年MM月DD日")
{
    year\_char\_index = val.indexOf(year\_char\_cn);
    month\_char\_index = val.indexOf(month\_char\_cn);
    day\_char\_index = val.indexOf(day\_char\_cn);
    if(onlydigits(val.substring(month\_char\_index+1,day\_char\_index)))
        d = parseInt(val.substring(month\_char\_index+1,day\_char\_index));
    if(onlydigits(val.substring(year\_char\_index+1,month\_char\_index)))
        m = parseInt(val.substring(year\_char\_index+1,month\_char\_index));
    if(onlydigits(val.substring(0,year\_char\_index)))
    {
        y = parseInt(val.substring(0,year\_char\_index));
        year = y;
    }
}
else if(window.dateformat == "EYY.MM.DD")
{
    comps = val.split(".");
    if(onlydigits(comps\[2\]))
        d = parseInt(comps\[2\]);
    if(onlydigits(comps\[1\]))
        m = parseInt(comps\[1\]);
    var era = comps\[0\].substring(0, 1);
    if(onlydigits(comps\[0\].substring(1,comps\[0\].length)))
    {
        y = get\_gregorian\_year(parseInt(comps\[0\].substring(1,comps\[0\].length)), era);
        year = y;
    }
}
else if(window.dateformat == "DD. Mon YYYY")
{
    comps = val.split(" ");
    if(onlydigits(comps\[0\].substring(0, comps\[0\].length - 1)))
        d = parseInt(comps\[0\].substring(0, comps\[0\].length - 1));
    m = getMonthIndex(comps\[1\]);
    if(onlydigits(comps\[2\]))
    {
        y = parseInt(comps\[2\]);
        year = y;
    }
}
else if(window.dateformat == "DD de MONTH de YYYY")
{
    comps = val.split(" de ");
    if(onlydigits(comps\[0\]))
        d = parseInt(comps\[0\]);
    m = arrayIndexOf(NLDate\_months, comps\[1\]) + 1;
    if(onlydigits(comps\[2\]))
    {
        y = parseInt(comps\[2\]);
        year = y;
    }
}
    else if(window.dateformat == "YYYY년 MM월 DD일")
{
    comps = val.split(" ");
    if(onlydigits(comps\[2\].substring(0, comps\[2\].length-1)))
        d = parseInt(comps\[2\].substring(0, comps\[2\].length-1));
    if(onlydigits(comps\[1\].substring(0, comps\[1\].length-1)))
        m = parseInt(comps\[1\].substring(0, comps\[1\].length-1)) - 1;
    if(onlydigits(comps\[0\].substring(0, comps\[0\].length-1)))
    {
        y = parseInt(comps\[0\].substring(0, comps\[0\].length-1));
        year = y;
    }
}
    else if(window.dateformat == "DD MONTH YYYY")
    {
            comps = val.split(" ");
            if(onlydigits(comps\[0\]))
                    d = parseInt(comps\[0\]);
            m = arrayIndexOf(NLDate\_months, comps\[1\], true) + 1;
            if(onlydigits(comps\[2\]))
            {
                    y = parseInt(comps\[2\]);
                    year = y;
            }
    }
    else if(window.dateformat == "DD MONTH, YYYY")
    {
            comps = val.split(" ");
            if(onlydigits(comps\[0\]))
                    d = parseInt(comps\[0\]);
            m = arrayIndexOf(NLDate\_months, comps\[1\].substring(0, comps\[1\].length-1), true) + 1;
            if(onlydigits(comps\[2\]))
            {
                    y = parseInt(comps\[2\]);
                    year = y;
            }
    }

if(m==0 || d==0)
{
    if(bDoAlert)
    {
        if(fmterr == "")
            fmterr = window.dateformat;
        alert("无效日期值(必须为"+window.dateformat+")");
    }
}
else
{
    if (y==0 && !onlydigits(year)) y = (new Date()).getFullYear();  
    if(m<1) m=1; else if(m>12) m=12;
    if(d<1) d=1; else if(d>31) d=31;
    if(y<100) y+=((y>=70)?1900:2000);
    if(y<1000) y\*=10;
    if (y > 9999) y = (new Date()).getFullYear();

    year = y;
            rtnDate = validateDate(new Date(y, m-1, d), bDoAlert);
}

return rtnDate;

}

个性化需求沟通 扫客服加V加群: