vue如何動態(tài)實時的顯示時間淺析
vue動態(tài)實時顯示時間有兩種方法
1.可以用day.js,處理日期和時間的js庫
用法 npm install dayjs --save
引入import dayjs from 'dayjs'
然后創(chuàng)建定時器更新最新的時間
this.timeId = setInterval(()=>{
this.sday =dayjs().format('YYYY-MM-DD HH:mm:ss');
}, 1000);
更多的詳情可以查看day.js的api
api文檔點這里
2.使用vue過濾器filters
<template>
<el-container id="box">
{{ date | formaDate }}
</el-container>
</template>
<script>
//創(chuàng)建一個函數(shù)來增加月日時小于10在前面加0
var padaDate = function(value){
return value<10 ? '0'+value : value;
};
export default {
data() {
return {
date: new Date(), //實時時間
};
},
watch: {
},
computed: {},
filters:{
//設置一個函數(shù)來進行過濾
formaDate:function(value){
//創(chuàng)建一個時間日期對象
var date = new Date();
var year = date.getFullYear(); //存儲年
var month = padaDate(date.getMonth()+1); //存儲月份
var day = padaDate(date.getDate()); //存儲日期
var hours = padaDate(date.getHours()); //存儲時
var minutes = padaDate(date.getMinutes()); //存儲分
var seconds = padaDate(date.getSeconds()); //存儲秒
//返回格式化后的日期
return year+'年'+month+'月'+day+'日'+hours+'時'+minutes+'分'+seconds+'秒';
}
},
methods: {
},
created() {
},
mounted() {
//創(chuàng)建定時器更新最新的時間
var _this = this;
this.timeId = setInterval(function() {
_this.sday =dayjs().format('YYYY-MM-DD HH:mm:ss');
}, 1000);
this.initmap();
},
beforeDestroy: function() {
//實例銷毀前青出于定時器
if (this.timeId) {
clearInterval(this.timeId);
}
}
};
</script>
<style lang="scss" scoped>
</style>
附:vue時間戳 獲取本地時間,實時更新
<template>
<p>當前時間:{{nowTime}}</p>
</template>
<script>
export default{
data(){
return{
nowTime:""
}
},
methods:{
getTime(){
setInterval(()=>{
//new Date() new一個data對象,當前日期和時間
//toLocaleString() 方法可根據(jù)本地時間把 Date 對象轉換為字符串,并返回結果。
this.nowtime = new Date().toLocaleString();
},1000)
}
},
created(){
this.getTime();
}
}
</script>

總結
到此這篇關于vue如何動態(tài)實時顯示時間的文章就介紹到這了,更多相關vue動態(tài)實時顯示時間內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue mock.js模擬數(shù)據(jù)實現(xiàn)首頁導航與左側菜單功能
這篇文章主要介紹了Vue mock.js模擬數(shù)據(jù)實現(xiàn)首頁導航與左側菜單功能,mockjs是用來模擬產生一些虛擬的數(shù)據(jù),可以讓前端在后端接口還沒有開發(fā)出來時獨立開發(fā)。我們可以使用真實的url,mockjs可以攔截ajax請求,返回設定好的數(shù)據(jù)2022-09-09
vue 使用axios 數(shù)據(jù)請求第三方插件的使用教程詳解
這篇文章主要介紹了vue 使用axios 數(shù)據(jù)請求第三方插件的使用 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
使用兩種方式調用本地json文件(基于Vue-cli3腳手架)
這篇文章主要介紹了使用兩種方式調用本地json文件(基于Vue-cli3腳手架),具有很好的參考價值,希望對大家有所幫助,2023-10-10

