vue关于时间的操作(持续更新)(时间格式化、获取当前系统时间)

Vue 显示时间

1. 实时获取系统时间

<template>
	<div>
        {{ currentDateTime }}
    </div>
</template>
export default {
	name: 'LockScreen',
	data () {
		return {
            currentDateTime: '',
		}
	},
    mounted () {
        this.getCurrentTime()
        setInterval(() => {
            this.getCurrentTime() // 每秒更新一次时间
        }, 1000)
    },
	methods: {
        getCurrentTime () {
            const date = new Date()
            this.currentTime = this.formatCurrentTime(date)
        },
		// 格式时间
        formatCurrentTime (date) {
            // 获取当前时间并打印
            const _this = this
            const yy = date.getFullYear()
            const mm = date.getMonth() + 1
            const dd = date.getDate()
            const hh = date.getHours()
            const mf = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
            const ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
            _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
            return _this.gettime
        }
	}
}

2. 格式化时间

2.1 封装全局过滤器

在utils中创建 filter.js 文件

const filter = {
  // value 时间
  // args  格式
  formatDate: function (value, args) {
    const dt = new Date(value)
    let year
    let month
    let date
    let hour
    let minute
    let second
    switch (args) {
      case 'yyyy-M-d' :
        year = dt.getFullYear()
        month = dt.getMonth() + 1
        date = dt.getDate()
        return `${year}-${month}-${date}`
      case 'yyyy-M-d H:m:s' :
        year = dt.getFullYear()
        month = dt.getMonth() + 1
        date = dt.getDate()
        hour = dt.getHours()
        minute = dt.getMinutes()
        second = dt.getSeconds()
        return `${year}-${month}-${date} ${hour}:${minute}:${second}`
      case 'yyyy-MM-dd':
        year = dt.getFullYear()
        month = (dt.getMonth() + 1).toString().padStart(2, '0')
        date = dt.getDate().toString().padStart(2, '0')
        return `${year}-${month}-${date}`
      case 'yyyy-MM-dd HH:mm:ss' :
        year = dt.getFullYear()
        month = (dt.getMonth() + 1).toString().padStart(2, '0')
        date = dt.getDate().toString().padStart(2, '0')
        hour = dt.getHours().toString().padStart(2, '0')
        minute = dt.getMinutes().toString().padStart(2, '0')
        second = dt.getSeconds().toString().padStart(2, '0')
        return `${year}-${month}-${date} ${hour}:${minute}:${second}`
    }
  }
}
export default filter

2.2 在 main.js 进行全局注入

import Vue from 'vue'
import filter from './utils/filter'

for (const key in filter) {
  Vue.filter(key, filter[key])
}

2.3 在其他页面使用

<template>
	<div>
        {{ new Date() | formatDate('yyyy-MM-dd HH:mm:ss') }}
    </div>
</template>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桂秋拾貳.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值