moment.js:http://momentjs.cn/
转换时间
js转换时间
| 1 | var date = new Date(value) | 
moment转换时间
| 1 | moment(new Date(value)).format('YYYYMMDDHHmmss'); | 
比较时间
| 1 | // 使用diff来比较时间 | 
倒计时
js倒计时
js倒计时方法1
| 1 | function formatSeconds(value) { | 
js倒计时方法2
| 1 | const diffSeconds = 7280 | 
moment倒计时
js:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23this.countTime(360000) // 不超过3600 00(100h)
// 将秒转换成时分秒(此处因实际需求将时分秒拆开显示)
exchangeTime (seconds) {
  const surplusDay = parseInt(seconds / 86400) // 天数取整
  const countH = moment('00:00:00', 'HH:mm:ss').add(seconds, 's').format('HH')
  this.countHH = surplusDay === 0 ? countH : (surplusDay * 24 + parseInt(countH))
  this.countmm = moment('00:00:00', 'HH:mm:ss').add(seconds, 's').format('mm')
  this.countss = moment('00:00:00', 'HH:mm:ss').add(seconds, 's').format('ss')
},
// 秒 倒计时
countTime (seconds) {
  const timmerHandler = setInterval(() => {
    if (seconds > 0) {
      seconds--
      this.exchangeTime(seconds)
    } else {
      seconds = 0
      clearInterval(timmerHandler)
    }
  }, 1000)
}