zoukankan      html  css  js  c++  java
  • js 转化为标准日期型

    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;
    }
    
  • 相关阅读:
    Flask—09-项目部署(01)
    Flask—08-建立自己的博客(02)
    Flask—07-建立自己的博客(01)
    Day 22:网络编程(3)
    Day 21:网络编程(2)
    Day 20:网络编程(1)
    Day 19:Properties配置文件类、打印流(printStream) 、 编码与解码
    Day 18:SequenceInputStream、合并切割mp3、对象输入输出流对象
    Day 17:缓冲输出字符流和用缓冲输入输出实现登录、装饰者设计模式
    Day 16:输入输出字符流、缓冲输入字符流
  • 原文地址:https://www.cnblogs.com/backuper/p/1619236.html
Copyright © 2011-2022 走看看