vue中的時間格式處理

方法一:自定義文件html

(一)函數封裝(將該函數封裝成一個文件,或者加入本身項目的函數庫)vue

(二)文件引入(注意:因爲是函數,故名字要和函數的名字一致)npm

import { formatDate } from '@/utils/date'

(三)添加到過濾器中函數

  filters: {
    formatDate(time) {
      let date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd')
    }
  }

(四)使用場景一: 在HTML中使用spa

// item.createdate是後臺數據~~
<div class="bottomTimee fz12 c_9a">{{item.createdate | formatDate}}</div>

(五)使用場景二: 在提交時候使用prototype

let nowDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm')

(六)使用場景三: 在綁定屬性中使用插件

<mt-cell title="開始時間" :value="startDate | formatDate"></mt-cell>

方法二:引入vue插件momentcode

(一)npm安裝orm

npm install moment --save

(二)在main.js上引入,掛載在vue上htm

  import moment from 'moment'
Vue.prototype.$moment = moment

(三)在html中使用

<div class="title">{{$moment(key).format('YYYY-MM-DD')}}</div>
相關文章
相關標籤/搜索