moment 计算两个时间差

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

 

    原文作者:正义儿
    原文地址: https://blog.csdn.net/qq_38364460/article/details/107084724
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞