vue時間戳和時間的相互轉(zhuǎn)換方式
vue時間戳和時間的相互轉(zhuǎn)換
一、時間戳轉(zhuǎn)化成時間
使用
<div>{{logTime|formatDate}}</div>
data:{ logTime:1609899674000000000 //這個是納秒的時間戳 }
filters: { formatDate: function (value) { let date = new Date(value / 1000000);//這個是納秒的,想要毫秒的可以不用除以1000000 let y = date.getFullYear(); let MM = date.getMonth() + 1; MM = MM < 10 ? ('0' + MM) : MM; let d = date.getDate(); d = d < 10 ? ('0' + d) : d; let h = date.getHours(); h = h < 10 ? ('0' + h) : h; let m = date.getMinutes(); m = m < 10 ? ('0' + m) : m; let s = date.getSeconds(); s = s < 10 ? ('0' + s) : s; return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; } },
二、時間轉(zhuǎn)換為時間戳
var startTime = new Date().getTime() * 1000000 - 60*60*1000*1000000; //這個是一天前的時間戳 var endTime = new Date().getTime() * 1000000; //轉(zhuǎn)換成納秒的時間戳 //注意:startTime --- endTime 可以得到當前的時間到一天前時間的范圍
這個可以用在element UI的日期時間選擇器中
operationTimeChange(val){ var start = new Date(val[0]).getTime() * 1000000; var end = new Date(val[1]).getTime() * 1000000; } //這個val是日期時間選擇器選擇時間后得到的
這是納秒的轉(zhuǎn)換,不乘以1000000得到的是毫秒的時間戳
補充:
vue3使用組合式api將時間戳格式轉(zhuǎn)換成時間格式(2023年09月28日 10:00)
場景1:直接轉(zhuǎn)換成某一個值formattedDate
在Vue 3中,你可以使用組合式API中的計算屬性來將13位時間戳轉(zhuǎn)換成指定格式的日期。下面是一個示例代碼:
<template> <div> <p>原始時間戳:{{ timestamp }}</p> <p>轉(zhuǎn)換后的日期:{{ formattedDate }}</p> </div> </template> <script> import { ref, computed } from 'vue'; export default { setup() { const timestamp = ref(1632800400000); // 13位時間戳,示例為2021年09月28日 10:00的時間戳 const formattedDate = computed(() => { const date = new Date(timestamp.value); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}年${month}月${day}日 ${hours}:${minutes}`; }); return { timestamp, formattedDate, }; }, }; </script>
在上面的代碼中,我們使用了ref來創(chuàng)建一個響應(yīng)式的timestamp變量,它存儲了13位時間戳。然后,我們使用computed來創(chuàng)建一個計算屬性formattedDate,它會根據(jù)timestamp的值動態(tài)計算出格式化后的日期。在formattedDate的計算函數(shù)中,我們使用new Date(timestamp.value)來將時間戳轉(zhuǎn)換成Date對象。然后,我們使用Date對象的方法來獲取年、月、日、小時和分鐘,并使用padStart方法來補齊位數(shù)。最后,我們將這些值拼接成指定格式的日期字符串,并返回給計算屬性。
場景2:將后臺返回的數(shù)組對象里面的所有時間戳準換成日期時間顯示在界面上:
<template> <div> <ul> <li v-for="(item, index) in items" :key="index"> <p>原始時間戳:{{ item.timestamp }}</p> <p>轉(zhuǎn)換后的日期:{{ formatDate(item.timestamp) }}</p> </li> </ul> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const items = ref([ { id: 1, timestamp: 1632800400000 }, { id: 2, timestamp: 1632811200000 }, { id: 3, timestamp: 1632822000000 }, ]); // 示例數(shù)據(jù),包含3個對象,每個對象都有一個13位時間戳字段 const formatDate = (timestamp) => { const date = new Date(timestamp); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}年${month}月${day}日 ${hours}:${minutes}`; }; return { items, formatDate, }; }, }; </script>
在上面的代碼中,我們使用ref來創(chuàng)建一個響應(yīng)式的items變量,它存儲了一個包含3個對象的數(shù)組,每個對象都有一個13位時間戳字段。然后,我們定義了一個formatDate函數(shù),它會將傳入的13位時間戳格式化成指定的日期字符串。
在模板中,我們使用v-for指令來遍歷items數(shù)組,并在每個對象的元素上展示原始時間戳和格式化后的日期。在展示格式化后的日期時,我們調(diào)用了formatDate函數(shù),并將13位時間戳作為參數(shù)傳入。
這樣,當我們運行上面的代碼時,就可以在界面上看到每個對象的原始時間戳和格式化后的日期了。
到此這篇關(guān)于vue時間戳和時間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)vue時間戳和時間轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+elementui+vuex+sessionStorage實現(xiàn)歷史標簽菜單的示例代碼
本文主要介紹了vue+elementui+vuex+sessionStorage實現(xiàn)歷史標簽菜單的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12Element Table 自適應(yīng)高度的實現(xiàn)示例
el-table的高度不能適應(yīng)不同電腦的分辨率,也不能跟隨瀏覽器的高度變化而變化的問題,本文就來解決一下Element Table 自適應(yīng)高度,感興趣的可以了解一下2024-07-07vue實現(xiàn)數(shù)據(jù)控制視圖的原理解析
這篇文章主要介紹了vue如何實現(xiàn)的數(shù)據(jù)控制視圖的相關(guān)知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01Vue3嵌套路由中使用keep-alive緩存多層的實現(xiàn)
本文介紹了Vue3 嵌套路由中使用?keep-alive緩存多層的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04基于Vue 實現(xiàn)一個中規(guī)中矩loading組件
這篇文章主要介紹了基于Vue 實現(xiàn)一個中規(guī)中矩loading組件,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04