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

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

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

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

最新評論