/// <summary> /// 计算两个日期的时间间隔 /// </summary> /// <param name="DateTime1">第一个日期和时间</param> /// <param name="DateTime2">第二个日期和时间</param> /// <returns></returns> private string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); if (ts.Days > 0) { dateDiff += ts.Days + "天 "; } if (ts.Hours > 0 || ts.Minutes > 0) { dateDiff += ts.Hours + Math.Round(((double)ts.Minutes/60)*100)/ 100 + "小时 "; } return dateDiff; }
function DateDiff(date1, date2) { var oRtnDate = {Days:0,Hours:0,Minutes:0,Seconds:0}; var dt1 = new Date(Date.parse(date1.replace(/-/g, '/')));//较早的日期时间 var dt2 = new Date(Date.parse(date2.replace(/-/g, '/')));//较晚的日期时间 var date3 = dt2.getTime() - dt1.getTime(); //计算出相差天数 var days = Math.floor(date3 / (24 * 3600 * 1000)) //计算出小时数 var leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / (3600 * 1000)) //计算相差分钟数 var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / (60 * 1000)) //计算相差秒数 var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数 var seconds = Math.round(leave3 / 1000) oRtnDate.Days = days; oRtnDate.Hours = hours; oRtnDate.Minutes = minutes; oRtnDate.Seconds = seconds; return oRtnDate; }