function CalcTimeSub(endTime, startTime, type) { var bool = endTime == defaultDateTime || startTime == defaultDateTime; var e = new Date(endTime); var s = new Date(startTime); var stime = Date.parse(s); var etime = Date.parse(e); var usedTime = etime - stime; //两个时间戳相差的毫秒数 var result = 0; switch (type) { case 's': if (bool) { return 0 + '秒'; } else { result = Math.floor(usedTime / 1000) + "秒"; } break; case 'm': if (bool) { return 0 + '分'; } else { result = Math.floor(usedTime / (60 * 1000)) + "分" + Math.floor(usedTime % (60 * 1000) / 1000) + "秒" } break; case 'H': if (bool) { return 0 + '时'; } else { result = Math.floor(usedTime / (60 * 60 * 1000)) + "时" + Math.floor(usedTime % (60 * 60 * 1000) / (60 * 1000)) + "分"; } break; case 'd': if (bool) { return 0 + '天'; } else { result = Math.floor(usedTime / (24 * 60 * 60 * 1000)) + "天" + Math.floor(usedTime % (24 * 60 * 60 * 1000) / (60 * 60 * 1000)) + "时"; } break; case 'M': if (bool) { return 0 + '月'; } else { result = Math.floor(usedTime / (30 * 24 * 60 * 60 * 1000)) + "月" + Math.floor(usedTime % (30 * 24 * 60 * 60 * 1000) / (24 * 60 * 60 * 1000)) + "天"; } break; case 'y': if (bool) { return 0 + '年'; } else { result = Math.floor(usedTime / (365 * 24 * 60 * 60 * 1000)) + "年" + Math.floor(usedTime % (365 * 30 * 24 * 60 * 60 * 1000) / (30 * 24 * 60 * 60 * 1000)) + "月"; } break; } return result; }