美文网首页
formatDate

formatDate

作者: Aiya_yaya | 来源:发表于2018-11-22 10:08 被阅读0次

后台返回时间格式化

1、import {formatDate} from '@comJs/data.js'

2、

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

3、表格的

<el-table-column prop="createDate" label="报送时间">  <template slot-scope="scope">      {{scope.row.createDate | formatDate}}  </template></el-table-column>

文本框的

 v-bind:value="showForm.redDate | formatDate"

转化时间一般使用new Date()

function format(date){

var data = new Date(date);

var year = date.getFullYear();

var month = date.getMonth() + 1;

var datee = date.getDate();

return year + ‘-’ + month + ‘-’ + datee

}

但是这种方法在IE上并不兼容,尤其是2019-04-25T16:00:00.000+000的时间格式,IE会直接返回NAN-NAN-NAN

因为我写代码用的是vue框架,查到vue插件moment可以转换时间格式,并且兼容IE,所以记录一下使用方法

安装:npm install moment --save

引入:import moment from ‘moment’(在使用页面直接引用即可)

使用:filters:{

formatDate(time) {

      const date = new Date(time);

      return moment(time).format('YYYY-MM-DD hh:mm:ss');

    },

}

相关文章

网友评论

      本文标题:formatDate

      本文链接:https://www.haomeiwen.com/subject/ftikqqtx.html