javascriptは二つの時間の差を計算します.

1366 ワード

 function GetDateDiff(startTime, endTime, diffType) {
            // xxxx-xx-xx     ,    xxxx/xx/xx    
            startTime = startTime.replace(/\-/g, "/");
            endTime = endTime.replace(/\-/g, "/");

            //              
            diffType = diffType.toLowerCase();
            var sTime = new Date(startTime);      //    
            var eTime = new Date(endTime);  //    
            //       
            var divNum = 1;
            switch (diffType) {
                case "second":
                    divNum = 1000;
                    break;
                case "minute":
                    divNum = 1000 * 60;
                    break;
                case "hour":
                    divNum = 1000 * 3600;
                    break;
                case "day":
                    divNum = 1000 * 3600 * 24;
                    break;
                default:
                    break;
            }
            return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
        }
//    
var result = GetDateDiff("2010-02-26 16:00:00", "2011-07-02 21:48:40", "day");