/** * 获取距离当前时间的 (天数/小时数/分钟数) * @param {*} timesData '2018-05-17 16:58:00' * @returns */ function timesDiff(timesData) { var dateBegin = new Date();//获取当前时间 var dateEnd = new Date(timesData.replace(/-/g, "/"));//将-转化为/,使用new Date var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数 var days = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数 var leave1 = dateDiff % (24 * 3600 * 1000) //计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / (3600 * 1000))//计算出小时数 var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / (60 * 1000))//计算相差分钟数 var diffObj = { days: 0, hours: 0, minutes: 0, }; if (days != 0) { diffObj.days = days; } else if (days == 0 && hours != 0) { diffObj.hours = hours; } else if (days == 0 && hours == 0) { diffObj.minutes = minutes; } return diffObj }