Vue获取实时时间并按需求格式化 发表于 2020-03-31 | 分类于 Web Technologies | 阅读次数: 本文字数: 1.6k | 阅读时长 ≈ 1 分钟 Vue获取实时时间并按需求格式化。 暂时记录一下代码,有空时再详细标注。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859<template> <div class="home"> <div class="showTime"> {{ dateFormat(date) }} </div> </div></template><script>export default { name: "Home", methods: { dateFormat(time) { var date = new Date(time); var year = date.getFullYear(); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); return ( year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds ); } }, data() { return { date: new Date() }; }, mounted() { var _this = this; this.timer = setInterval(function() { _this.date = new Date(); }, 1000); }, beforeDestroy: function() { if (this.timer) { clearInterval(this.timer); } }};</script> 本文作者: 异名者 本文链接: https://Yimingzhe.github.io/2020/03/31/web08/ 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!