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)文章
如何使用Webpack優(yōu)化Vue項(xiàng)目的打包流程
在開發(fā)基于Vue.js的應(yīng)用時(shí),隨著項(xiàng)目規(guī)模的擴(kuò)大,單個(gè)文件的體積也會(huì)隨之增長,特別是當(dāng)涉及到大量的依賴庫和復(fù)雜的業(yè)務(wù)邏輯時(shí),本文將詳細(xì)介紹如何使用Webpack來優(yōu)化Vue項(xiàng)目的打包流程,需要的朋友可以參考下2024-09-09
在vue+element-plus中無法同時(shí)使用v-for和v-if的問題及解決方法
由于路由中存在不需要遍歷的數(shù)據(jù)所以像用v-if來過濾,但是報(bào)錯(cuò),百度說vue不能同時(shí)使用v-if和v-for,今天小編給大家分享解決方式,感興趣的朋友跟隨小編一起看看吧2023-07-07
Vue中@click.stop與@click.prevent解讀
Vue中,`@click.stop`用于阻止事件冒泡,而`@click.prevent`用于阻止事件的默認(rèn)行為,這兩個(gè)方法在處理事件時(shí)非常有用2025-02-02
Vue自動(dòng)構(gòu)建發(fā)布腳本的方法示例
這篇文章主要介紹了Vue自動(dòng)構(gòu)建發(fā)布腳本的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue3實(shí)現(xiàn)長列表虛擬滾動(dòng)的示例代碼
本文主要介紹了vue3實(shí)現(xiàn)長列表虛擬滾動(dòng)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Vue3中ref方法和reactive方法的區(qū)別解析及哪個(gè)更高效
Vue3中的ref和reactive是創(chuàng)建響應(yīng)式數(shù)據(jù)的核心API,它們在適用數(shù)據(jù)類型、訪問方式、底層實(shí)現(xiàn)和適用場景上存在顯著差異,ref適用于任何類型的值,而reactive僅適用于對象或數(shù)組,下面通過本文給大家介紹Vue3中ref方法和reactive方法的區(qū)別,感興趣的朋友跟隨小編一起看看2026-01-01

