When we want to vue Get the latest time in real time , You can use the following methods :
<script> export default { data() { return { timer: "", // Define a variable of timer currentTime
: new Date().getFullYear() + "-" + new Date().getMonth() + 1 + "-" + new Date().
getDate() + " " + new Date().getHours() + ":" + new Date().getMinutes() + ":" +
new Date().getSeconds(), // Get current time } }, created() { var vm = this vm.timer =
setInterval(() => { var y = new Date().getFullYear() var m = vm.appendZero(new
Date().getMonth() + 1) var d = vm.appendZero(new Date().getDate()) var ho = vm.
appendZero(new Date().getHours()) var mi = vm.appendZero(new Date().getMinutes()
) var se = vm.appendZero(new Date().getSeconds()) // Modify data date vm.currentTime = y
+ "-" + m + "-" + d + " " + ho + ":" + mi + ":" + se }, 1000) }, methods: {
// Time filter plus 0 appendZero(obj) { if (obj < 10) { return "0" + obj } else { return obj
} }, }, beforeDestroy() { if (this.timer) { clearInterval(this.timer) //
stay Vue Before instance destruction , Clear our timer } }, } </script>
Then use it in the template :
Technology