最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue將后臺數(shù)據(jù)時間戳轉(zhuǎn)換成日期格式

 更新時間:2019年07月31日 15:39:37   作者:詩人的貓咪  
這篇文章主要為大家詳細(xì)介紹了vue將后臺數(shù)據(jù)時間戳轉(zhuǎn)換成日期格式,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

在項(xiàng)目中,經(jīng)常會有后臺返回一個時間戳,頁面展示用的卻是日期格式的情況
不同組件多次使用的話,那么建議在 src 下新建一個 common 文件夾,創(chuàng)建 date.js 文件,方便多次復(fù)用

在組件中使用

<template>
 <div>
  <p>{{date1 | formatDate}}</p>
  <p>{{date1 | formatDate2}}</p>
  <p>{{date1 | formatDate3}}</p>
 </div>
</template>
<script>
 import { formatDate } from '@/common/date.js' // 在組件中引用date.js
 export default {
  data() {
   return {
    date1: 1646461131351
   }
  },
  filters: {
   /*
    時間格式自定義 只需把字符串里面的改成自己所需的格式
   */ 
   formatDate(time) {
    var date = new Date(time);
    return formatDate(date, 'yyyy.MM.dd'); 
   },
   formatDate2(time) {
    var date = new Date(time);
    return formatDate(date, 'hh:mm:ss'); 
   },
   formatDate3(time) {
    var date = new Date(time);
    return formatDate(date, 'yyyy年MM月dd日 hh:mm:ss'); 
   }
  }
 }
</script>

效果圖

date.js源碼

export function formatDate(date, fmt) {
 if (/(y+)/.test(fmt)) {
  fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
 }
 let o = {
  'M+': date.getMonth() + 1,
  'd+': date.getDate(),
  'h+': date.getHours(),
  'm+': date.getMinutes(),
  's+': date.getSeconds()
 };
 for (let k in o) {
  if (new RegExp(`(${k})`).test(fmt)) {
   let str = o[k] + '';
   fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
  }
 }
 return fmt;
};

function padLeftZero(str) {
 return ('00' + str).substr(str.length);
};

如果本篇文章對你有幫助的話,很高興能夠幫助上你。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

峨眉山市| 囊谦县| 五河县| 上饶市| 罗田县| 宣城市| 泰来县| 清水县| 清流县| 河曲县| 新邵县| 姚安县| 读书| 双峰县| 曲松县| 德安县| 黄大仙区| 阜平县| 南京市| 荣昌县| 崇文区| 肇东市| 祁阳县| 渭源县| 宁夏| 淮北市| 普陀区| 绥中县| 和顺县| 大丰市| 新疆| 宜黄县| 盖州市| 阿瓦提县| 长宁区| 隆化县| 邯郸县| 晋宁县| 武冈市| 菏泽市| 商都县|