js计算相差时间

使用传入的两个时间的毫秒差来计算时间

1.计算相差天数

/**
 * 若是要计算从开始时间到结束时间共几天的话需要加1
 * startDate: 开始时间;格式: YYYY-MM-DD 或者 YYYY-MM-DD hh:mm:ss
 * endDate: 结束时间;格式: YYYY-MM-DD 或者 YYYY-MM-DD hh:mm:ss
 * */
const DifferDayTime = function(startDate, endDate) { // 一天等于86400000毫秒
	return Math.floor((new Date(endDate).getTime() - new Date(startDate).getTime()) / 86400000)
}

2.计算相差小时数

const DifferHourTime = function(startDate, endDate) { // 一小时等于3600000毫秒
	return Math.floor((new Date(endDate).getTime() - new Date(startDate).getTime()) / 3600000)
}

3.计算相差分钟数

const DifferMinuteTime = function(startDate, endDate) { // 一分钟等于60000毫秒
	return Math.floor((new Date(endDate).getTime() - new Date(startDate).getTime()) / 60000)
}

4.计算相差秒数

const DifferSecondTime = function(startDate, endDate) { // 一秒等于1000毫秒
	return Math.floor((new Date(endDate).getTime() - new Date(startDate).getTime()) / 1000)
}

你可能感兴趣的:(jsvascript,javascript,vue.js,前端,reactjs,node.js)