<script>
var d=new Date("2007","9","12","12");
var now=new Date();
var pass=parseInt((now.getTime()-d.getTime())/1000);
alert("已经过去"+parseInt(pass/60/60)+"小时"+parseInt(pass/60)%60+"分钟"+pass%60+"秒钟");
</script>

解决方案 »

  1.   

    <script language=javascript>
    var str="2007-10-11 15:00:00"
    var re=/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/
    if(!re.test(str))
    alert("输入错误")
    var strYear=RegExp.$1;
    var strMonth=RegExp.$2;
    strMonth=parseInt(strMonth,10)-1
    var strDate=RegExp.$3;
    strDate=parseInt(strDate,10)
    var strHour=RegExp.$4;
    strHour=parseInt(strHour,10)
    var strMinute=RegExp.$5;
    strMinute=parseInt(strMinute,10)
    var tempDate=new Date(strYear,strMonth,strDate,strHour,strMinute,0)
    var nowDate=new Date()
    var temp=parseInt((nowDate-tempDate)/1000)
    var StrSeconds=temp%60
    temp=parseInt(temp/60)
    var strMinute=temp%60
    temp=parseInt(temp/60)
    var strHour=temp%24
    temp=parseInt(temp/24)
    document.write(temp+"天"+strHour+"小时"+strMinute+"分"+StrSeconds+"秒")
    </script>
      

  2.   

     <div id="div1">&nbsp;</div>
     <script language=javascript >
    var str="2007-10-11 15:00:00"
    var re=/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/
    if(!re.test(str))
    alert("输入错误")
    var strYear=RegExp.$1;
    var strMonth=RegExp.$2;
    strMonth=parseInt(strMonth,10)-1
    var strDate=RegExp.$3;
    strDate=parseInt(strDate,10)
    var strHour=RegExp.$4;
    strHour=parseInt(strHour,10)
    var strMinute=RegExp.$5;
    strMinute=parseInt(strMinute,10)
    var tempDate=new Date(strYear,strMonth,strDate,strHour,strMinute,0)
    function showDate(){
    var nowDate=new Date()
    var temp=parseInt((nowDate-tempDate)/1000)
    var StrSeconds=temp%60
    temp=parseInt(temp/60)
    var strMinute=temp%60
    temp=parseInt(temp/60)
    var strHour=temp%24
    temp=parseInt(temp/24)
    document.getElementById("div1").innerText=(temp+"天"+strHour+"小时"+strMinute+"分"+StrSeconds+"秒")
    setTimeout("showDate()",1000)
    }
    showDate()
    </script >
      

  3.   

    <body><div id="dd"></div>
    <script language="JavaScript">
    <!--
    function mm(strDate)
    {
      var d = Date.parse(strDate.replace(/-/g, "/"));
      if(isNaN(d)){alert("date string is wrong!"); return;}
      d = new Date().getTime() - d; var s="", n;
      if((n=Math.floor(d/(1000*60*60*24)))>0) s += n +"日";
      d %=(1000*60*60*24);
      if((n=Math.floor(d/(1000*60*60)))>0) s += n +"时";
      d %= (1000*60*60);
      if((n=Math.floor(d/(1000*60)))>0) s += n +"分";
      d %= (1000*60);
      if((n=Math.floor(d/(1000)))>0) s += n +"秒";
      return s;
    }
    setInterval(function(){document.getElementById("dd").innerHTML = mm("2007-10-11 12:00:00")}, 1000)
    //-->
    </script>
      

  4.   

    加上 Math.abs() 还可以倒计时:
    <body><div id="dd"></div>
    <script language="JavaScript">
    <!--
    function mm(strDate)
    {
      var d = Date.parse(strDate.replace(/-/g, "/"));
      if(isNaN(d)){alert("date string is wrong!"); return;}
      d = new Date().getTime() - d; var s="", n, i=1000*60*60*24;
      if(Math.abs(n=Math.floor(d/i))>0) s += n +"日"; d %= i; i=1000*60*60;
      if(Math.abs(n=Math.floor(d/i))>0) s += n +"时"; d %= i; i=1000*60;
      if(Math.abs(n=Math.floor(d/i))>0) s += n +"分"; d %= i; i=1000;
      if(Math.abs(n=Math.floor(d/i))>0) s += n +"秒";
      return s;
    }
    setInterval(function(){document.getElementById("dd").innerHTML = mm("2007-10-12 12:00:00")}, 1000)
    //-->
    </script>