no message
Former-commit-id: 393b0dcb5b150dba8c885950b4e7ff014694b0a3 Former-commit-id: 474f4843c60b88e4caafcf172c30e47a07efd93b Former-commit-id: 8c7c236ef02b6ebe012640406ff1dc48e11c6290 Former-commit-id: 2baf360fdad8b6026a87bf379cf48e0e18d06bbb
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
# 时间差计算
|
||||
|
||||
``` js
|
||||
import timeago from 'timeago.js'
|
||||
export default {
|
||||
data () {
|
||||
return {
|
||||
// 打开页面时间
|
||||
openPageDate: new Date(),
|
||||
// 打开页面已经过去的时间
|
||||
openPageDateAgo: '',
|
||||
// 打开页面时间计时器
|
||||
dateTimeRangeTimer: null,
|
||||
// 起止时间
|
||||
dateTimeRange: [new Date(2018, 0, 1, 0, 0), new Date()],
|
||||
// 上面起止时间的计算结果
|
||||
dateTimeRangeAgo: ''
|
||||
}
|
||||
},
|
||||
mounted () {
|
||||
// 刷新打开页面过去的时间
|
||||
this.refreshOpenPageDateAgo()
|
||||
this.dateTimeRangeTimer = setInterval(this.refreshOpenPageDateAgo, 1000)
|
||||
// 刷新起止时间的计算结果
|
||||
this.refreshDateTimeRangeAgo()
|
||||
},
|
||||
beforeDestroy () {
|
||||
// 清空计时器
|
||||
clearInterval(this.dateTimeRangeTimer)
|
||||
},
|
||||
watch: {
|
||||
dateTimeRange () {
|
||||
// 刷新起止时间的计算结果
|
||||
this.refreshDateTimeRangeAgo()
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 计算打开页面过了多久
|
||||
refreshOpenPageDateAgo () {
|
||||
this.openPageDateAgo = timeago().format(this.openPageDate, 'zh_CN')
|
||||
},
|
||||
// 计算起止时间间隔
|
||||
refreshDateTimeRangeAgo () {
|
||||
const timeagoInstance = timeago(this.dateTimeRange[1])
|
||||
this.dateTimeRangeAgo = timeagoInstance.format(this.dateTimeRange[0], 'zh_CN')
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user