如题 比如 2010-01-31 10:00:00 跟 2010-02-01 10:00:00 怎么计算

解决方案 »

  1.   

    var date1="2010-01-31 10:00:00";
    var date2="2010-02-01 10:00:00";
    var diff=new Date(date1.replace("-", "/"))-new Date(date2.replace("-", "/"));
    var diffMin=diff/1000;
    diffMin就是两个日期相差的秒数。再从秒数计算分钟,小时等
      

  2.   

    前面的解决了,还有个问题是var now = new Date();
                    var year = now.getFullYear();
                    var month = (now.getMonth() + 1) < 10 ? "0" + (now.getMonth() + 1) : (now.getMonth() + 1);
                    var day = now.getDate();
                    var hours = now.getHours();
                    var min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
                    var ss = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
                    document.getElementById("txtStartTime").value = year + "-" + month + "-" + ((day - 1) < 10 ? "0" + (day - 1) : (day - 1)) + " " + hours + ":" + min + ":" + ss;
                    document.getElementById("txtEndTime").value = year + "-" + month + "-" + (day< 10 ? "0" + day : day ) + " " + hours + ":" + min + ":" + ss;
                    document.getElementById("divHistory").style.display = "block";
    我想得到的效果是 开始时间是 2010-01-31 00:00:00
     结束时间是 2010-02-01 00:00:00可是我减1号开始时间是 2010-01-02-00 00:00:00?
       该怎么弄
      

  3.   

    function dateDiff(interval,date1,date2)
        {
            var objInterval = {'D' : 1000 * 60 * 60 * 24, 'H' : 1000 * 60 * 60,
                               'M' : 1000 * 60, 'S' : 1000, 'T' : 1};
            interval = interval.toUpperCase();
            var dt1 = Date.parse(date1.replace(/-/g, '/'));
            var dt2 = Date.parse(date2.replace(/-/g, '/'));
            try
            {
                return Math.round((dt2 - dt1) / eval('(objInterval.' + interval + ')'));
            }
            catch (e)
            {
                return e.message;
            }
        }
    用这函数吧。
      

  4.   

    dateDiff('D',start_day,end_day)就是计算他们相差的天数。