验证yyyyMMdd日期字符串是否合法(起止时间)

/**
 * 验证yyyyMMdd时间字符串是否合法
 * @param beginTime
 * @param endTime
 * @param str
 * @returns {boolean}
 */
function checkDateStartandEnd(beginTime,endTime,str){
    if(beginTime == "" || endTime == ""){
        error(str+"格式错误");
        return false;
    }else if(beginTime != ""&& endTime != ""){
        if(toDateFromString(beginTime)&&toDateFromString(endTime)){
            if(parseInt(beginTime)>parseInt(endTime)){
                error(str+"起时间不能大于止时间!");
                return false;
            }
        }else {
            error(str+"格式错误");
            return false;
        }
    }else{
        return true;
    }
}

/**
 * 验证(yyyyMMdd)格式的字符串是否符合时间字符串要求
 * @param strDate
 * @returns {boolean}
 */
function toDateFromString( strDate )
{
    if (strDate.length != 8) {
        return false ;
    }
    var dtDate = null ;
    var nYear = parseInt( strDate.substring( 0, 4 ), 10 ) ;
    var nMonth = parseInt( strDate.substring( 4, 6 ), 10 ) ;
    var nDay = parseInt( strDate.substring( 6, 8 ), 10 ) ;
    if( isNaN( nYear ) == true || isNaN( nMonth ) == true || isNaN( nDay ) == true )
    {
        return false ;
    }
    dtDate = new Date( nYear, nMonth - 1, nDay ) ;
    if( nYear != dtDate.getFullYear() || ( nMonth - 1 ) != dtDate.getMonth() || nDay != dtDate.getDate() )
    {
        return false ;
    }
    return true ;
}

猜你喜欢

转载自blog.csdn.net/qq_36178165/article/details/85259792