vue中對(duì)時(shí)間戳的處理方式
vue對(duì)時(shí)間戳的處理
1、自建js文件
文件位置根據(jù)自己的項(xiàng)目位置自定義
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); };
2、在組件模板中引用定義好的時(shí)間戳函數(shù)
<template> ? ? <div>{{time | formatDate}}</div> </template>
3、設(shè)置好在script中的引用和定義
<script> import {formatDate} from 'xxx.js'; export default { ? ? filters: { ? ? ? ? formatDate(time) { ? ? ? ? ? ? var date = new Date(time); ? ? ? ? ? ? return formatDate(date, 'yyyy-MM-dd hh:mm'); ? ? ? ? } ? ? } } </script>
vue時(shí)間戳轉(zhuǎn)換
后臺(tái)拿時(shí)間數(shù)據(jù),一般不是時(shí)間格式,而是一串?dāng)?shù)組,這個(gè)時(shí)候就需要轉(zhuǎn)換,然后才可以使用了
?add_time(row, column, cellValue, index) { ? ? ? ? if (cellValue == null || cellValue == "") return ""; ? ? ? ? let date = new Date(parseInt(cellValue) * 1000); ? ? ? ? let Y = date.getFullYear() + '-'; ? ? ? ? let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-'; ? ? ? ? let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '; ? ? ? ? let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'; ? ? ? ? let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':'; ? ? ? ? let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); ? ? ? ? return Y + M + D; ? ? ? },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue如何使用moment處理時(shí)間戳轉(zhuǎn)換成日期或時(shí)間格式
- vue3時(shí)間戳轉(zhuǎn)換(不使用過(guò)濾器)
- Vue過(guò)濾器filters的用法及時(shí)間戳轉(zhuǎn)換問(wèn)題
- vue中根據(jù)時(shí)間戳判斷對(duì)應(yīng)的時(shí)間(今天 昨天 前天)
- vue將后臺(tái)數(shù)據(jù)時(shí)間戳轉(zhuǎn)換成日期格式
- vue獲取時(shí)間戳轉(zhuǎn)換為日期格式代碼實(shí)例
- Vue.js 時(shí)間轉(zhuǎn)換代碼及時(shí)間戳轉(zhuǎn)時(shí)間字符串
相關(guān)文章
Vue3 computed初始化獲取設(shè)置值實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Vue3 computed初始化以及獲取值設(shè)置值實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10vue-cil之a(chǎn)xios的二次封裝與proxy反向代理使用說(shuō)明
這篇文章主要介紹了vue-cil之a(chǎn)xios的二次封裝與proxy反向代理使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04如何解決ElementPlus的el-table底白線問(wèn)題
這篇文章主要介紹了如何解決ElementPlus的el-table底白線問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Monaco?Editor開(kāi)發(fā)SQL代碼提示編輯器實(shí)例詳解
這篇文章主要為大家介紹了Monaco?Editor開(kāi)發(fā)SQL編輯器實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08解決vue的 v-for 循環(huán)中圖片加載路徑問(wèn)題
今天小編就為大家分享一篇解決vue的 v-for 循環(huán)中圖片加載路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue.js?前端項(xiàng)目在常見(jiàn)?Web?服務(wù)器上的部署配置過(guò)程
Web服務(wù)器支持多種編程語(yǔ)言,如 PHP,JavaScript,Ruby,Python 等,并且支持動(dòng)態(tài)生成 Web 頁(yè)面,這篇文章主要介紹了Vue.js?前端項(xiàng)目在常見(jiàn)?Web?服務(wù)器上的部署配置,需要的朋友可以參考下2023-02-02