• 判斷日期,輸出成YYYY/MM/DD形式


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>轉換日期</title>
    <script type="text/javascript">
    String.prototype.IsDate
    =function(){
        
    var dateReg=/^[1-3]\d{3}(\/|-)?((1[0-2])|(0?[1-9]))(\/|-)?((0?[1-9])|([1-2]\d)|(3[01]))$/;
        
    var rdelimiter="/";
        
    var nDate;
        
    if(dateReg.test(this)){
            
    var iDate=this.replace(/\//g,"").replace(/-/,"");
            if(iDate.length == 6){
                nDate 
    = iDate.substr(04)+rdelimiter+"0"+iDate.substr(41)+rdelimiter+"0"+iDate.substr(51);
            }
    else{
                
    var nMonth = parseInt(iDate.substr(42));
                
    if(nMonth > 12){    //月份為一位
                    nDate = iDate.substr(04)+rdelimiter+"0"+iDate.substr(41)+rdelimiter+iDate.substr(52);
                }
    else{
                    
    var nDay = "0"+iDate.substr(62);
                    nDate 
    = iDate.substr(04)+rdelimiter+"0"+iDate.substr(42)+rdelimiter+"0"+nDay.substr(nDay.length-22);
                }
            }
            
    if(isNaN(new Date(nDate))){
                
    return false;
            }
    else{
                
    return true;
            }
        }
    else{
            
    return false;
        }
    }
    /**//*********************************************************
    **    名稱:getFormatDate()                                **
    **  功能:格式化日期                                    **
    **    參數: iDate        :轉換前日期YYYY-M/D或YYYY/MM/DD    **
    **         idelimiter    :轉換前日期分格符                    **
    **         rdelimiter    :轉換後的分格符,默認為"/"            **
    **    返回: String,YYYY+分格符+MM+分格符+DD                **
    ********************************************************
    */
    function getFormatDate(iDate, idelimiter, rdelimiter){
        
    var regStr;
        
    var delimiter;
        
    var tDate;
        
    var rDate;
        
    var rMonth;
        
    var rDay;
        
    if(rdelimiter == ""){
            rdelimiter
    ="/";
        }
        
    //替換掉分隔符
        for(var i=0; i<idelimiter.length; i++){
            delimiter
    =idelimiter.substr(i, 1);
            
    if(delimiter == "/"){
                iDate
    =iDate.replace(/\//g, "");
            }else{
                regStr 
    = "iDate=iDate.replace(/"+delimiter+"/g, '');";
                eval(regStr);
            }
        }
        
    if(iDate.length == 6){
            nDate 
    = iDate.substr(04)+rdelimiter+"0"+iDate.substr(41)+rdelimiter+"0"+iDate.substr(51);
        }
    else{
            
    var nMonth = parseInt(iDate.substr(42));
            
    if(nMonth > 12){    //月份為一位
                nDate = iDate.substr(04)+rdelimiter+"0"+iDate.substr(41)+rdelimiter+iDate.substr(52);
            }
    else{
                
    var nDay = "0"+iDate.substr(62);
                nDate 
    = iDate.substr(04)+rdelimiter+"0"+iDate.substr(42)+rdelimiter+"0"+nDay.substr(nDay.length-22);
            }
        }
        
        tDate
    =new Date(nDate);
        
    if(isNaN(tDate)){
            
    throw("日期非法");
        }
        rMonth 
    = "00"+(tDate.getMonth()+1).toString();
        rMonth 
    = rMonth.substr(rMonth.length-22);
        rDay 
    = "00"+tDate.getDate().toString();
        rDay 
    = rDay.substr(rDay.length-22);
        rDate 
    = tDate.getFullYear().toString() + rdelimiter + rMonth + rdelimiter + rDay;
        
    return rDate;
    }
    function test(){
        
    var cText=document.getElementById("test");
        
    if(cText.value.IsDate()){
            cText.value
    =getFormatDate(cText.value, "/-""/");
            
    return true;
        }
    else{
            alert(
    "日期格式不正確或非法,正確輸入格式是YYYY/MM/DD或YYYYMMDD");
            
    return false;
        }
    }
    </script>
    </head>

    <body>
    <input type="text" id="test" />
    <input type="button" value="測試" onclick="test()" />
    </body>
    </html>
  • 相关阅读:
    【常用配置】Spring框架web.xml通用配置
    3.从AbstractQueuedSynchronizer(AQS)说起(2)——共享模式的锁获取与释放
    2.从AbstractQueuedSynchronizer(AQS)说起(1)——独占模式的锁获取与释放
    1.有关线程、并发的基本概念
    0.Java并发包系列开篇
    SpringMVC——DispatcherServlet的IoC容器(Web应用的IoC容器的子容器)创建过程
    关于String的问题
    Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)
    <<、>>、>>>移位操作
    System.arraycopy(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别
  • 原文地址:https://www.cnblogs.com/ywkpl/p/1325553.html
Copyright © 2020-2023  润新知