vue中時間格式化組件庫moment.js的使用

1 首先在vue項目中

npm install moment

import moment from "moment";
Vue.prototype.$moment = moment;

Vue.filter("dateformat", function(value, formatString) {
  formatString = formatString || "YYYY-MM-DD";
  // return moment(value).format(formatString);
  return moment(value).format(formatString); // 這是時間戳轉(zhuǎn)時間
});

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

3 只需要在需要格式化時間的地方使用插值表達式就OK了


  <!-- 子標題 -->
      <p class="subtitle">
          <span>發(fā)表時間:{{ newsinfo.add_time | dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
          <span>點擊{{ newsinfo.click }}次</span>
     </p>

第二種方法
引入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')

官網(wǎng)鏈接http://momentjs.cn/docs/

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

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

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