欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue時(shí)間戳和時(shí)間的相互轉(zhuǎn)換方式

 更新時(shí)間:2023年12月22日 09:27:43   作者:再希  
本文通過示例代碼介紹了vue時(shí)間戳和時(shí)間的相互轉(zhuǎn)換方式,通過場景分析介紹了vue3使用組合式api將時(shí)間戳格式轉(zhuǎn)換成時(shí)間格式(2023年09月28日 10:00),感興趣的朋友一起看看吧

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)文章

最新評論