src\main.js中引入commonMethod.js
import CommonMethod from './util/commonMethod'
Vue.use(CommonMethod)
src\util\commonMethod.js
export default {
install(Vue, options) {
Vue.prototype.dateFormat = (date, format) => {
if (date == null || date == '') {
return ''
}
else {
//IE不支持'-'
var _date = date.split('-').join('/')
var d = new Date(_date)
var month = (parseInt(d.getMonth() + 1)) < 10 ? ('0' + parseInt(d.getMonth() + 1)) : parseInt(d.getMonth() + 1)
var day = d.getDate() < 10 ? ('0' + d.getDate()) : d.getDate()
var hour = d.getHours() < 10 ? ('0' + d.getHours()) : d.getHours()
var minute = d.getMinutes() < 10 ? ('0' + d.getMinutes()) : d.getMinutes()
var second = d.getSeconds() < 10 ? ('0' + d.getSeconds()) : d.getSeconds()
if (format == 'dateTime') {
return d.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
}
else { //'date'
return d.getFullYear() + '-' + month + '-' + day
}
}
}
}
}