VUE使用day.js顯示時(shí)分秒并實(shí)時(shí)更新時(shí)間效果實(shí)例
效果:
1.安裝dayjs
npm install dayjs
2.項(xiàng)目中引入
import dayjs from 'dayjs'
data(){ return { timeId: null, weeks: ['日', '一', '二', '三', '四', '五', '六'], day: dayjs().format('YYYY/MM/DD'),//格式化年月日 time: dayjs().add(1, 'second').format('HH:mm:ss'), //格式化時(shí)分秒,用add方法的原因是因?yàn)? week: dayjs().day(),//獲取星期幾 } }, computed: { //獲得星期幾的漢字 dWeek() { return this.weeks[this.week + 0] } }, mounted() { this.timeId = setInterval(() => { this.time = dayjs().add(1, 'second').format('HH:mm:ss'); }, 1000); }, beforeDestroy() { //組件銷毀前清楚定時(shí)器 if (this.timeId) { clearInterval(this.timeId) } },
模板中使用
<div class="time_text">{{time}}</div>t <div class="data_text"> <span>星期{{dWeek}}</span> <span>{{day}}</span> </div>
示例代碼
<template> <div> <div class="time_text">{{time}}</div> <div class="data_text"> <span>星期{{dWeek}}</span> <span>{{day}}</span> </div> </div> </template> <script> import dayjs from 'dayjs' export default { data () { return { timeId: null, weeks: ['日', '一', '二', '三', '四', '五', '六'], day: dayjs().format('YYYY/MM/DD'),//格式化年月日 time: dayjs().add(1, 'second').format('HH:mm:ss'), //格式化時(shí)分秒,用add方法的原因是因?yàn)? week: dayjs().day(),//獲取星期幾 } }, computed: { //獲得星期幾的漢字 dWeek () { return this.weeks[this.week + 0] } }, mounted () { this.timeId = setInterval(() => { this.time = dayjs().add(1, 'second').format('HH:mm:ss'); }, 1000); }, beforeDestroy () { //組件銷毀前清楚定時(shí)器 if (this.timeId) { clearInterval(this.timeId) } }, } </script> <style lang="scss" scoped> //代碼樣式根據(jù)需要調(diào)整 </style>
附:day.js 將時(shí)間轉(zhuǎn)換為年月日時(shí)分秒
安裝 npm install dayjs --save 想要使用的頁(yè)面引入 var dayjs = require('dayjs') var dayjsTime = dayjs(`${new Date()}`).format('YYYY-MM-DD HH:mm:ss') // new Date() 換成自己需要轉(zhuǎn)換的數(shù)據(jù) console.log(dayjsTime) // 2019-10-12 15:19:28
總結(jié)
到此這篇關(guān)于VUE使用day.js顯示時(shí)分秒并實(shí)時(shí)更新時(shí)間效果的文章就介紹到這了,更多相關(guān)VUE day.js顯示時(shí)分秒內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用moment將GMT時(shí)間轉(zhuǎn)換為北京時(shí)間
GMT(Greenwich Mean Time)和UTC(Coordinated Universal Time)是兩個(gè)時(shí)間標(biāo)準(zhǔn),它們?cè)谠S多方面非常相似,但也有一些微小的差異,本文給大家詳細(xì)介紹了Vue使用moment將GMT時(shí)間轉(zhuǎn)換為北京時(shí)間,需要的朋友可以參考下2023-12-12vue使用element-resize-detector監(jiān)聽元素寬度變化方式
這篇文章主要介紹了vue使用element-resize-detector監(jiān)聽元素寬度變化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12uniapp前端支付篇之微信、抖音、快手、h5四個(gè)平臺(tái)支付功能
支付功能在我們?nèi)粘i_發(fā)中經(jīng)常會(huì)遇到,下面這篇文章主要給大家介紹了關(guān)于uniapp前端支付篇之微信、抖音、快手、h5四個(gè)平臺(tái)支付功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03vue項(xiàng)目移動(dòng)端實(shí)現(xiàn)ip輸入框問題
這篇文章主要介紹了vue項(xiàng)目移動(dòng)端實(shí)現(xiàn)ip輸入框問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03vue使用@scroll監(jiān)聽滾動(dòng)事件時(shí),@scroll無效問題的解決方法詳解
這篇文章主要介紹了vue使用@scroll監(jiān)聽滾動(dòng)事件時(shí),@scroll無效問題的解決方法,結(jié)合實(shí)例形式分析了@scroll監(jiān)聽滾動(dòng)事件無效問題的原因及相應(yīng)的解決方法,需要的朋友可以參考下2019-10-10Vue.js條件渲染和列表渲染以及Vue中key值的內(nèi)部原理
這篇文章主要介紹了Vue.js條件渲染和列表渲染,以及Vue中key值的內(nèi)部原理,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考閱讀2023-04-04