Date.parse("2003-4-6".replace(/-/g,'/'))

解决方案 »

  1.   

    function DateDemo(){
       var d, s = "今天日期是: ";
       d = new Date();
       s += (d.getYear() + 1) + "-";
       s += d.getMonth() + "-";
       s += d.getDate();
       return(s);
    }
      

  2.   

    谢谢net_lover,但我看到很多网站填日期格式不对都会弹出对话框提示你?他们是用分析字符串的方法吗
      

  3.   

    <SCRIPT LANGUAGE=javascript>
    <!--
    aa="2002-12-10"
    bb="4321.rw.34"
    execScript("i=IsDate(aa)","vbscript")
    if(i)alert("aa的格式正确")
    else alert("aa的格式不正确,请更正")execScript("i=IsDate(bb)","vbscript")
    if(i)alert("bb的格式正确")
    else alert("bb的格式不正确,请更正")//-->
    </SCRIPT>最简单的办法,try please
      

  4.   

    搜索javascript论坛。里面有许多解决方案
      

  5.   

    to zhjzh_zjz:
    谢谢您!我试过可以。这是用VBScript的东西吗,JS自身没有这个功能?
    但是我只是用IE可以,在JBuilder里的开发环境里却不会执行,是不是JBuilder不支持VBScript?有没有办法解决呢
    而且我试过"2003/4/6"的格式也能通过,还有没有其他格式也会通过?若我只想"2003-4-6"
    通过,其他都不能通过,可以达到吗??
      

  6.   

    if  (isInvalidDate(register.bday.value,"-")==true)  
           alert("请正确填写您的出生日期(例如:1981-11-12)!");  
    我在精华区找到上面的代码,是否就能实现第1问的功能?
    但运行时有错误,说是缺少对象。何解?
      

  7.   

    因为isInvalidDate这个函数不是JS自带的,是自定义的。
      

  8.   

    转摘+修改:
    只能判断日期,日期后面不能跟有时间//**********************************************************************************************************
    //功能:日期检查函数,支持3种年、月、日之间的分隔符 "年月日"、"-"、"."和"/"可以选择年、月、日是否应该完整。
    //正确的日期格式为:2001-2-13 2001 2001-2 2001.2.13  2001.2 2001/2/3,日期范围为 1-1-1 到 9999-12-31
    //同时,对当前年当前月的天数也做了判断,如:2001-2-29 2001-4-31 都是非法的日期
    //参数:strDate ---- 需要判断的日期字符串
    //intFlag: 1 ---- 可以没有日  2 ---- 可以没有日和月 0 ---- 年月日必须齐全
    //返回值:true ---- 日期合法 false ---- 日期不合法
    function fnIsDate(strDate,intFlag)
    {
    var strCheckDate = fnTrim(strDate) + "";     //进一步确认哪来判断的肯定是一串字符串 if(strCheckDate == "")        //空字符串,不是合法的日期字符串,返回false
    {
    return false;
    } //判断传进来的数据是那种格式写成日期
    var intIndex = -1;         //利用正则表达式,查找字符串中是否包含某个字符,没找到为-1,否则为 (0 - String.length - 1)
    var arrDate;          //分别存储年月日
    var regExpInfo = /\./;        //正则表达式,匹配第一个出现 "."的位置 //在这里,我之所以不使用replace函数把所有的"."和"/"换成"-",然后分别存储年月日,是因为用户有可能输入 2001/3-2,就判断不出它是不合法日期了
    intIndex = strCheckDate.search(regExpInfo);   //查找是否含有 "."
    if(intIndex == -1)         //不包含
    {
    regExpInfo = /-/;
    intIndex = strCheckDate.search(regExpInfo);   //查找是否含有 "-" if(intIndex == -1)
    {
    regExpInfo = /\//;       //查找是否含有 "/"
    intIndex = strCheckDate.search(regExpInfo); if(intIndex == -1)
    {
    regExpInfo = /((.+)年$|(.+)年(.+)月$|(.+)年(.+)月(.+)日$)/;
    intIndex = strCheckDate.search(regExpInfo); if(intIndex == -1)
    {
    arrDate = new Array(strCheckDate);  //只包含年
    }
    else
    {
    if(RegExp.$2 != "") //只包含年
    {
    arrDate = new Array(RegExp.$2);
    }
    else if(RegExp.$3 != "") //包含年月
    {
    arrDate = new Array(RegExp.$3, RegExp.$4);
    }
    else //包含3个
    {
    arrDate = new Array(RegExp.$5, RegExp.$6, RegExp.$7);
    }
    }
    }
    else
    {
    arrDate = strCheckDate.split("/");  //2001/3/7 型
    }
    }
    else
    {
    arrDate = strCheckDate.split("-");   //2001-3-7 型
    }
    }
    else
    {
    arrDate = strCheckDate.split(".");    //2001.3.7 型
    } if(arrDate.length > 3)        //如果分离出来的项超过3,除了年月日还有其它的,不合法日期,返回false
    {
    return false;
    }
    else if(arrDate.length > 0)
    {
    //判断年是否合法
    if(fnIsIntNum(arrDate[0]))   //是正整数
    {
    if(parseInt(arrDate[0]) < 1 || parseInt(arrDate[0]) > 9999)  //年范围为1 - 9999
    {
    return false;
    }
    }
    else
    {
    return false;     //年不是正整数,错误
    } //判断月是否合法
    if(arrDate.length > 1)
    {
    if(fnIsIntNum(arrDate[1]))  //是正整数
    {
    if(parseInt(arrDate[1]) < 1 || parseInt(arrDate[1]) > 12)
    {
    return false;
    }
    }
    else
    {
    return false;
    }
    }
    else //没有月
    {
    if(intFlag != 2)    //必须得有月
    {
    return false;
    }
    } //判断日是否合法
    if(arrDate.length > 2)
    {
    if(fnIsIntNum(arrDate[2]))  //是正整数
    {
    var intDayCount = fnComputerDay(parseInt(arrDate[0]),parseInt(arrDate[1]));
    if(intDayCount < parseInt(arrDate[2]))
    {
    return false;
    }
    }
    else
    {
    return false;
    }
    }
    else
    {
    if(intFlag == 0)    //必须得有日
    {
    return false;
    }
    }
    }
    return true;
    }//**********************************************************************************************************
    //判断一个数是否为正整数
    //参数:strNum ---- 需要判断的字符串
    //返回值:true ---- 整数 false ---- 非整数
    function fnIsIntNum(strNum)
    {
    var strCheckNum = strNum + "";
    if(strCheckNum.length < 1)         //空字符串
    return false;
    else if(isNaN(strCheckNum))         //不是数值
    return false;
    else if(parseInt(strCheckNum) < 1)       //不是正数
    return false;
    else if(parseFloat(strCheckNum) > parseInt(strCheckNum)) //不是整数
    return false; return true;
    }//**********************************************************************************************************
    //功能:判断intYear年intMonth月的天数
    //返回值:intYear年intMonth月的天数
    function fnComputerDay(intYear,intMonth)
    {
    var dtmDate = new Date(intYear,intMonth,-1);
    var intDay = dtmDate.getDate() + 1; return intDay;
    }//********************************************************************************************************** //功能:去掉字符串前后空格
    //返回值:去掉空格后的字符串
    function fnTrim(strSource)
    {
    return strSource.replace(/^\s*/,'').replace(/\s*$/,'');
    }
      

  9.   

    这是我实现的取前一天(月、年、季)和后一天(月、年、季)的函数:function GetPrevDay(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate();
    if (sDay > 1) sDay --;
    else
    {
    if (sMonth > 1) sMonth --; else {sYear --;sMonth = 12;};
    if (sMonth == 2) sDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) sDay = 30; else sDay = 31;
    }
    return new Date(sYear,sMonth-1,sDay);
    }
    function GetNextDay(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    if (sDay < lastDay) sDay ++; else {sDay = 1; if (sMonth < 12) sMonth ++; else {sMonth = 1; sYear ++;}}
    return new Date(sYear,sMonth-1,sDay);
    }
    function GetPrevMonth(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    if (sMonth > 1) sMonth --; else {sMonth = 12; sYear --;}
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    return new Date(sYear,sMonth-1,sDay<lastDay?sDay:lastDay);
    }
    function GetNextMonth(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    if (sMonth < 12) sMonth ++; else {sMonth = 1; sYear ++;}
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    return new Date(sYear,sMonth-1,sDay<lastDay?sDay:lastDay);
    }
    function GetPrevYear(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    sYear--;
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    return new Date(sYear,sMonth-1,sDay<lastDay?sDay:lastDay);
    }
    function GetNextYear(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    sYear++;
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    return new Date(sYear,sMonth-1,sDay<lastDay?sDay:lastDay);
    }
    function GetPrevQuarter(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    if (sMonth > 3) sMonth -= 3; else {sMonth += 9; sYear --;}
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    return new Date(sYear,sMonth-1,sDay<lastDay?sDay:lastDay);
    }
    function GetNextQuarter(sDate)
    {
    var sYear = sDate.getFullYear(),sMonth = sDate.getMonth()+1,sDay = sDate.getDate(),lastDay;
    if (sMonth < 10) sMonth += 3; else {sMonth -= 9; sYear ++;}
    if (sMonth == 2) lastDay = (((sYear % 4) == 0) && ((sYear % 100) != 0) || ((sYear % 400) == 0))?29:28; else if (sMonth == 4 || sMonth == 6 || sMonth == 9 || sMonth == 11) lastDay = 30; else lastDay = 31;
    return new Date(sYear,sMonth-1,sDay<lastDay?sDay:lastDay);
    }