日期:2014-05-17 浏览次数:20559 次
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是字符串 yyyy-MM-dd格式
var aDate, oDate1, oDate2, iDays, ihours, iminutes, iseconds;
aDate = sDate1.split("-");
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);//转换为MM-dd-yyyy格式
aDate = sDate2.split("-");
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
var timeSpan = {};
var TotalMilliseconds = Math.abs(oDate1 - oDate2);//相差的毫秒数
timeSpan.Days = parseInt(TotalMilliseconds / 1000 / 60 / 60 /24);
timeSpan.TotalHours = parseInt(TotalMilliseconds / 1000 / 60 / 60);
timeSpan.Hours = timeSpan.TotalHours % 24;
timeSpan.TotalMinutes = parseInt(TotalMilliseconds / 1000 / 60);
timeSpan.Minutes = timeSpan.TotalMinutes % 60;
timeSpan.TotalSeconds = parseInt(TotalMilliseconds / 1000);
timeSpan.Seconds = timeSpan.TotalSeconds % 60;
timeSpan.TotalMilliseconds = TotalMilliseconds;
timeSpan.Milliseconds = TotalMilliseconds % 1000;
return timeSpan;
}