在vue中使用moment.js格式化時(shí)間

一: 安裝

npm install moment --save

二:在main.js中 導(dǎo)入組件

import moment from 'moment'

三:在main.js定義全局過(guò)濾器

Vue.filter('dateformat', function(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {

return moment(dataStr).format(pattern)

})

filter兩個(gè)參數(shù) 第一個(gè)是函數(shù)名 ?第二個(gè)是時(shí)間格式化處理的函數(shù)

四:在需要格式化時(shí)間的地方使用插值表達(dá)式

? ? ? ? ? <span>發(fā)表時(shí)間:{{ item.date | dateformat('YYYY-MM-DD HH:mm:ss')}}</span>

? ? ? ? ? <span>點(diǎn)擊{{ item.click }}次</span>




第二種方法:


引入moment.js

npm install moment --save

直接在所需要的組件中引入就ok了

<script>

let moment = require("moment");


export default {

data() {

return {

}

可以直接使用了

if(this.ruleForm2.startTime == '' || this.ruleForm2.startTime == null || this.ruleForm2.startTime == undefined){

this.ruleForm2.startTime = ''

}else {

this.ruleForm2.startTime = moment(this.ruleForm2.startTime).format('YYYY-MM-DD')

}

如果是想要 轉(zhuǎn)化為年月日分秒

this.ruleForm2.startTime = moment(this.ruleForm2.startTime).format('YYYY-MM-DD HH-mm')

</script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容