vue 日期过滤器

这篇博客介绍了在Vue.js中创建全局日期过滤器的方法,用于格式化日期显示。提供了两种不同的实现方式,一种是直接在过滤器中处理日期格式,另一种是通过传入自定义格式参数来实现。示例代码展示了如何将日期转换为'yyyy-MM-dd'格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@vue 日期过滤器

/*
全局时间过滤器
使用方法 e.g {{ [item.EntityEndTime,‘yyyy-MM-dd’] | Date}}
*/
Vue.filter(‘Date’, (item) => {
const oDate = new Date(item[0]);
const o = {
‘M+’: oDate.getMonth() + 1, // 月份
‘d+’: oDate.getDate(), // 日
‘h+’: oDate.getHours(), // 小时
‘m+’: oDate.getMinutes(), // 分
‘s+’: oDate.getSeconds(), // 秒
‘q+’: Math.floor((oDate.getMonth() + 3) / 3), // 季度
S: oDate.getMilliseconds(), // 毫秒
};
// eslint-disable-next-line no-restricted-syntax
if (/(y+)/.test(item[1])) { item[1] = item[1].replace(RegExp.1,(‘1, (`1,({oDate.getFullYear()}).substr(4 - RegExp.$1.length)); } for (const k in o) { if (new RegExp((${k})).test(item[1])) item[1] = item[1].replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((00o[k]‘).substr((‘{o[k]}`).substr((`o[k]).substr(({o[k]}`).length))); }
return item[1];
});

第二种 :

使用 :/** {{item.dataTime | dateFormat(‘yyyy-MM-dd’)}} */
Vue.filter(‘dateFormat’, function(originVal,fmt) {
const dt = new Date(originVal)

const y = dt.getFullYear()
const m = (dt.getMonth() + 1 + ‘’).padStart(2, ‘0’)
const d = (dt.getDate() + ‘’).padStart(2, ‘0’)

const hh = (dt.getHours() + ‘’).padStart(2, ‘0’)
const mm = (dt.getMinutes() + ‘’).padStart(2, ‘0’)
const ss = (dt.getSeconds() + ‘’).padStart(2, ‘0’)

/*可以自己设置/
if(fmt === ‘yyyy-MM-dd’){
return ${y}-${m}-${d}
}

return ${y}-${m}-${d} ${hh}:${mm}:${ss}
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值