可以在Model 层直接计算出相差毫秒数
public double Duration { get { if (EndTime != null && StartTime != null) { return EndTime.Value.Subtract(StartTime.Value).TotalSeconds; } else { return 0; } } }
然后前台取到这个Duration字段
把他换算为时分秒
function UserTime(duration) { var theTime = parseInt(duration);// 秒 var middle = 0;// 分 var hour = 0;// 小时 if (theTime > 60) { middle = parseInt(theTime / 60); theTime = parseInt(theTime % 60); if (middle > 60) { hour = parseInt(middle / 60); middle = parseInt(middle % 60); } } var result = "" + parseInt(theTime) + "秒"; if (middle > 0) { result = "" + parseInt(middle) + "分" + result; } if (hour > 0) { result = "" + parseInt(hour) + "小时" + result; } return result; }