例如两个时间,格式是这样的,2010-06-09T20:48:52Z和2010-06-10T21:16:20Z,请问有什么好的方法算出时间差?
谢谢

解决方案 »

  1.   

    var t = new Date("2010-06-10T21:16:20Z") - new Date("2010-06-09T20:48:52Z");
    alert(["相差", Math.floor(t / 1000), "秒"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60), "分钟"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60 / 60), "小时"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60 / 60 / 24), "天"].join(""));
      

  2.   

    多谢楼上,可是我运行的时候,结果都是显示NaN,这是怎么回事?
      

  3.   

    <script>
    var str1 = "2010-06-09T20:48:52Z";
    var str2 = "2010-06-10T21:16:20Z";
    str1 = str1.replace(/-/g,"/").replace(/T/g," ").replace(/Z/g,"");
    str2 = str2.replace(/-/g,"/").replace(/T/g," ").replace(/Z/g,"");
    var t = new Date(str2) - new Date(str1);
    alert(["相差", Math.floor(t / 1000), "秒"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60), "分钟"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60 / 60), "小时"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60 / 60 / 24), "天"].join(""));
    </script>
      

  4.   

    没想到IE不支持这种格式。先处理成ie能识别的格式“2010/06/10 21:16:20”
    function parseDate(str) {
        return new Date(str.replace(/(\d{4})[-\/]?(\d{1,2})[-\/]?(\d{1,2})[ T]?(\d{1,2}):(\d{1,2}):(\d{1,2})Z?/, 
            "$1/$2/$3 $4:$5:$6"));
    }var t = parseDate("2010-06-10T21:16:20Z") - parseDate("2010-06-09T20:48:52Z");
    alert(["相差", Math.floor(t / 1000), "秒"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60), "分钟"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60 / 60), "小时"].join(""));
    alert(["相差", Math.floor(t / 1000 / 60 / 60 / 24), "天"].join(""));
      

  5.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function show() {
    var time = "2010-06-09T20:48:52Z";
    var times = "2010-06-10T21:16:20Z";
    var stemp = time.split("T");
    var stemps = times.split("T");
    var arry = stemp[1].split("Z");
    var arrys = stemps[1].split("Z");
    var arryTime = new Array();

    arryTime[0] = stemps[0] - stemp[0]//年月日
    arryTime[1] = arrys[0] - arry[0]//小时分秒
    alert(arryTime.join(" ",arryTime));


    }
    //-->
    </SCRIPT>
     </HEAD> <BODY onLoad="show()">
      
     </BODY>
    </HTML>