vue-fliters (时间格式化)

vue-fliters  

记录下时间戳格式化,直接贴码

步骤1:    import  dataFliter from "@xxxxx.data.js"

步骤2

export default {

data() {

          return {};

  },

filters: {//记得加在这里

     dataFliter

},

};

扫描二维码关注公众号,回复: 4542646 查看本文章

步骤3.使用 eg : <template><span>{{detailData.createTime | dataFliter('yyyy/MM/dd hh:mm')}}</span></template>

data.js  文件如下

const dateFmt = (value, fmt) => {

if (value) {

value = new Date(value);

var o = {

'M+': value.getMonth() + 1, // 月份

'd+': value.getDate(), // 日

'h+': value.getHours(), // 小时

'm+': value.getMinutes(), // 分

's+': value.getSeconds(), // 秒

'q+': Math.floor((value.getMonth() + 3) / 3), // 季度

'S': value.getMilliseconds() // 毫秒

};

if (/(y+)/.test(fmt)) {

fmt = fmt.replace(RegExp.$1, (value.getFullYear() + '').substr(4 - RegExp.$1.length));

}

for (var k in o) {

if (new RegExp('(' + k + ')').test(fmt)) {

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));

}

}

return fmt;

}

};

export default dateFmt;

补充:  格式化 刚刚,2小时前等

msgTime.js

const fmtMsgTime = (timespan) => {

if (timespan) {

var dateTime = new Date(timespan)

var year = dateTime.getFullYear()

var month = dateTime.getMonth() + 1

var day = dateTime.getDate()

var hour = dateTime.getHours()

var minute = dateTime.getMinutes()

var second = dateTime.getSeconds()

var now = new Date()

// var nowNew = Date.parse(now.toDateString())

var nowNew = Date.parse(now)

var milliseconds = 0

var timeSpanStr

milliseconds = nowNew - timespan

if (milliseconds <= 1000 * 60 * 1) {

timeSpanStr = '刚刚'

} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {

timeSpanStr = Math.round(milliseconds / (1000 * 60)) + '分钟前'

} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {

timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前'

} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {

timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前'

} else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year === now.getFullYear()) {

timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute

} else {

timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second

}

return timeSpanStr

}

}

export default fmtMsgTime


 

猜你喜欢

转载自blog.csdn.net/Bruce__taotao/article/details/81115094