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