<template>
<div class="hello">
{{time}}
{{times}}
</div>
</template>
<script>
import moment from 'moment'
export default {
data () {
return {
time: (new Date()).valueOf(),
times: null
}
},
methods: {
getTime () {
let s = new Date(this.time)
console.log(s, 's==')
this.times = moment(s).format('HH:mm:ss')
let hour = (s.getHours()).toString().padStart(2, '0')
let minutes = (s.getMinutes()).toString().padStart(2, '0')
let seconds = (s.getSeconds()).toString().padStart(2, '0')
console.log('当前时间为:' + hour + ':' + minutes + ':' + seconds)
},
toTime () {
let sTime = new Date(this.time).valueOf()
setInterval(() => {
let sTime = new Date()
let eTime = new Date('Dec 10 2021 00:00:00')
let residue = eTime.getTime() - sTime.getTime()
let residueDay = parseInt(residue / (60 * 60 * 24 * 1000)).toString().padStart(2, '0')
let haomiao1 = parseInt(residue) - parseInt(residueDay * 60 * 60 * 1000)
let residueHour = parseInt(haomiao1 / (60 * 60 * 1000)).toString().padStart(2, '0')
let haomiao2 = haomiao1 - residueHour * 60 * 60 * 1000
let residueMinute = parseInt(haomiao2 / (60 * 1000)).toString().padStart(2, '0')
let haomiao3 = haomiao2 - residueMinute * 60 * 1000
let haomiao4 = parseInt(haomiao3 / 1000)
this.times = residueDay + '天' + residueHour + '小时' + residueMinute + '分钟' + haomiao4 + '秒'
}, 1000)
if (sTime < 0) {
this.times = '倒计时结束'
}
}
},
mounted () {
this.getTime()
this.toTime()
}