问一个 判断时间的代码 最好是 JS版的
 要求  输入时间 必须大于或者等于今天 否则不让输入
时间格式2008-05-09

解决方案 »

  1.   

    没人回答我来回答吧。。
    JS先获取当前时间
    var year=now.getYear();
    var month=now.getMonth();
    var date=now.getDate();
    var nd = year + "-" + month + "-" + date;
    var ld = 输入时间;
    nd = new Date(nd.replace("-", "/")); 
    ld = new Date(ld.replace("-", "/")); 
    if (Date.parse(ld) - Date.parse(nd ) < 0) {
    alert("输入时间小于当前时间");
    return false;
    }   以上代码没测试过。楼主试下
      

  2.   

     var now=new Date();
         var year=now.getYear();
                var month=now.getMonth();
                var date=now.getDate();
                var nd = year + "-" + month + "-" + date;
                var ld = "2008-5-24";
                nd = new Date(nd.replace("-", "/")); 
                ld = new Date(ld.replace("-", "/")); 
                if (Date.parse(ld) - Date.parse(nd ) < 0) {
                alert("输入时间小于当前时间");
                }   else{
                    alert("输入时间大于当前时间");
                }
    不好意思。刚才now忘记定义了 你再试下
      

  3.   

    日期格式2008-05-09像这种,直接去掉-,比较大小就行了
    var times= "2008-05-09:;
    times = times.replace("-","");
    把当前日期也同上去掉-,然后两个数直接对比
      

  4.   

    month月份要加1
    又忘记了
     var now=new Date(); 
         var year=now.getYear(); 
                var month=now.getMonth(); 
                var date=now.getDate(); 
                month = month +1;
                var nd = year + "-" + month + "-" + date; 
                var ld = "2008-5-24"; 
                nd = new Date(nd.replace("-", "/"));  
                ld = new Date(ld.replace("-", "/"));  
                if (Date.parse(ld) - Date.parse(nd )  < 0) { 
                alert("输入时间小于当前时间"); 
                }   else{ 
                    alert("输入时间大于当前时间"); 
                } 
      

  5.   


    var _date = document.getElementById("txtDate").value;
    if(!/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(_date)){
    alert("日期格式不正确");
    return;
    }
    var now = new Date();
    var now_days = now.getYear()*365+now.getMonth()*30+now.getDate();
    var input_days = parseInt(_date.split('-')[0])*365+parseInt(_date.split('-')[1])*30+parseInt(_date.split('-')[2]);
    if(input_days<now_days){
    alert("输入日期不能小于当前日期");
    return;
    }
      

  6.   

    //getMonth 方法返回一个处于 0 到 11 之间的整数,它代表 Date 对象中的月份值。这个整数并不等于按照惯例来表示月份的数字,而是要比按惯例表示的值小 1。如果一个 Date 对象中保存的时间值是 "Jan 5, 1996 08:47:00",那么 getMonth 方法就会返回 0。
      

  7.   

    我试下
    var a=/^(\d{4})\-(\d{1,2})\-{\d{1,2}}$/g;
    var b=*//你获得的日期 就是用户自己输入的
    var t=new Date();
    var year=t.getYear();
    var month=t.getDate();
    var day=t.getDay();
    var str
    result=b.match(a);
    len=
    if(result!=null)
    {
    if (parseInt(result[0])>=year)
    {return true;
    }
    else
    {
    return false;
    }
    if(parseInt(result[0])==year&&parseInt(result[1]>=month))
    {return true;}
    else
    {return false;}
    if(parseInt(result[0])==year&&parseInt(result[1]==month)&&parseInt(result[2])>=day)
    {...
    }
    else
    {...}}
    else
    {return false;}
      

  8.   

       
                function test()
                {
                var now=new Date();  
                var year=now.getYear();  
                var month=now.getMonth();  
                var date=now.getDate();  
                month = month +1; 
                var nd = year + "-" + month + "-" + date;  
                var ld = "2008-5-24";  
                nd = ld.replace("-",""); 
                ld = ld.replace("-",""); 
                 if (Date.parse(ld) - Date.parse(nd )   < 0) {  
                alert("输入时间小于当前时间");  
                }   else{  
                    alert("输入时间大于当前时间");  
                }              }
    //这段代码,我试了下FF也可以的
    如果不行看下是不是你的FF问题
      

  9.   

    这代码在 IE里  不管ld输入多少 都是大于  汗
      

  10.   

    我晕 大家帮帮忙呀  就没有支持IE 额 FF的代码吗
      

  11.   

    后台C#验证
    try
    {
    Convert.ToDateTime(this.txtStartTime.Text);
    if(Convert.ToDateTime(this.txtStartTime.Text).ToShortDateString().CompareTo(DateTime.Now.ToShortDateString())==0 || Convert.ToDateTime(this.txtStartTime.Text).ToShortDateString().CompareTo(DateTime.Now.ToShortDateString())==1)
    {
    //执行代码
    }
    }
    catch
    {
    Response.Write("<script>alert()</script>");
    Response.End();
    }
      

  12.   

    试了 好久才发现FF里 var now=new Date();   
                var year=now.getYear();   
                var month=now.getMonth();   
                var date=now.getDate();   
                month = month +1;  
                var nd = year + "-" + month + "-" + date;   
    取不到时间
      

  13.   

    function verifyDateTime(strData)
    {
        var pattern = /^\s*((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))\s*$/;
        if(pattern.test(strData))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
      

  14.   

     <script type="text/javascript">
               var now=new Date();
                var year=now.getFullYear(); 
                var month=now.getUTCMonth()+1;
                var date=now.getDate();
               // month = month +1; 
                var nd = year + "-" + month + "-" + date; 
                alert(nd); 
                var ld = "2008-5-28"; 
                alert(ld);
            
                if (ld<nd) 
                {  
                alert("输入时间小于当前时间");  
                 } 
               else
               {  
                 alert("输入时间大于当前时间");  
    --------------------------------------
    ff下测试通过!
               }  
    </script>
      

  15.   

    ----可替换----
        var month=now.getMonth()+1;
      

  16.   

    我想没那么麻烦吧,说下我的想法,就直接用数字比较不行吗?比如,20080509>20080508,我想我已经把怎么做的说出来了吧,至于里面怎么做,一是要把2008-05-09分割,然后,2008*10000+05*100+09,我想够简单了吧.
    PS:LS的都是牛人,小弟佩服,人笨,觉得没那必要搞那么复杂吧/
      

  17.   

    忘说了,时间CHECK还是要的,这个JS的,网上一大把,就不说了/