vue实现全局(局部)日期过滤器(任意格式)

1.全局日期过滤器

<script>
	Vue.filter('formaDate2', function (value, fmt) {
     
     
            var date = new Date(value);
            var o = {
     
     
                "M+": date.getMonth() + 1, //月份
                "d+": date.getDate(), //日
                "h+": date.getHours(), //小时
                "m+": date.getMinutes(), //分
                "s+": date.getSeconds(), //秒
                "w+": date.getDay(), //星期
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o) {
     
     
                if (k === 'w+') {
     
     
                    if (o[k] === 0) {
     
     
                        fmt = fmt.replace('w', '周日');
                    } else if (o[k] === 1) {
     
     
                        fmt = fmt.replace('w', '周一');
                    } else if (o[k] === 2) {
     
     
                        fmt = fmt.replace('w', '周二');
                    } else if (o[k] === 3) {
     
     
                        fmt = fmt.replace('w', '周三');
                    } else if (o[k] === 4) {
     
     
                        fmt = fmt.replace('w', '周四');
                    } else if (o[k] === 5) {
     
     
                        fmt = fmt.replace('w', '周五');
                    } else if (o[k] === 6) {
     
     
                        fmt = fmt.replace('w', '周六');
                    }
                } else 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;
        })
</script>

由于测试,所以我在data中定义的nowTime: new Date()
使用如下(常用几种格式):
在这里插入图片描述

<p>{
   
   {nowTime | formaDate('yyyy-MM-dd')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy-MM-dd hh:mm:ss')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy-MM-dd hh:mm:ss w')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy/MM/dd')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy/MM/dd hh:mm:ss')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy/MM/dd hh:mm:ss w')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy年MM月dd日')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy年MM月dd日 hh:mm:ss')}}</p>
<p>{
   
   {nowTime | formaDate('yyyy年MM月dd日 hh:mm:ss w')}}</p>

2.局部日期过滤器
使用方法和全局使用方法一样(同上)

<script>
	export default{
     
     
		  data(){
     
     
		   return{
     
     
                nowTime: new Date()
            },
            // 局部过滤器
            filters: {
     
     
                formaDate2(value, fmt) {
     
     
                    var date = new Date(value);
                    var o = {
     
     
                        "M+": date.getMonth() + 1, //月份
                        "d+": date.getDate(), //日
                        "h+": date.getHours(), //小时
                        "m+": date.getMinutes(), //分
                        "s+": date.getSeconds(), //秒
                        "w+": date.getDay(), //星期
                        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                        "S": date.getMilliseconds() //毫秒
                    };
                    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                    for (var k in o) {
     
     
                        if (k === 'w+') {
     
     
                            if (o[k] === 0) {
     
     
                                fmt = fmt.replace('w', '周日');
                            } else if (o[k] === 1) {
     
     
                                fmt = fmt.replace('w', '周一');
                            } else if (o[k] === 2) {
     
     
                                fmt = fmt.replace('w', '周二');
                            } else if (o[k] === 3) {
     
     
                                fmt = fmt.replace('w', '周三');
                            } else if (o[k] === 4) {
     
     
                                fmt = fmt.replace('w', '周四');
                            } else if (o[k] === 5) {
     
     
                                fmt = fmt.replace('w', '周五');
                            } else if (o[k] === 6) {
     
     
                                fmt = fmt.replace('w', '周六');
                            }
                        } else 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;
                }
            }
       }
	}
}
</script>

猜你喜欢

转载自blog.csdn.net/qq_45695853/article/details/115242689
今日推荐