1.安装引用moment
import moment from ‘moment’
2.以秒为单位计算时分秒
// 起始时间
let startTime = "2020-07-02 14:40:23"
// 结束时间(当前时间)
let endTime = moment().format("YYYY-MM-DD HH:mm:ss")
// 计算两个时间相差秒数(时间差单位可以是years,months,days,minutes,seconds)
let timeDiff = moment(endTime).diff(moment(startTime), "seconds")
// 小时
let timeH = Math.floor(timeDiff / 3600)
timeH = timeH < 10 ? "0" + timeH : timeH
// 分钟
let timeMinute = Math.floor((timeDiff % 3600) / 60)
timeMinute = timeMinute < 10 ? "0" + timeMinute : timetimeMinuteH
// 秒
let timeSeconds = (timeDiff % 3600) % 60
timeSeconds = timeSeconds < 10 ? "0" + timeSeconds : timeSeconds
3.单位是分
let timeDiff = moment(endTime).diff(moment(startTime), "minute")
// 小时
let timeH = Math.floor(timeDiff / 60)
timeH = timeH < 10 ? "0" + timeH : timeH
// 分钟
let timeMinute = timeDiff % 60
timeMinute = timeMinute < 10 ? "0" + timeMinute : timetimeMinuteH