vue格式化时间

该文章展示了一个使用Vue.js创建的时间格式化过滤器,无需额外插件,可以直接在项目中应用。通过示例代码,展示了如何将不同格式的时间戳转换为多种易读的日期格式,包括年月日、小时分钟秒以及星期等信息。

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

效果图

简单实用,不需要下载插件,在项目中也可以这样用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>格式化时间</title>
    <style>
        #app{
            font-weight: bold;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div>{{timeOne}} ----> {{timeOne | format}}</div> <!--默认格式-->
        <div>{{timeTwo}} ----> {{timeTwo | format('MM-DD')}}</div>
        <div>{{timeTwo}} ----> {{timeTwo | format('MM月DD日')}}</div>
        <div>{{timeTwo}} ----> {{timeTwo | format('YYYY/MM/DD')}}</div>
        <div>{{timeOne}} ----> {{timeTwo | format('YYYY-MM-DD')}}</div>
        <div>{{timeTwo}} ----> {{timeTwo | format('YYYY/MM/DD HH:mm:ss 星期w')}}</div>
        <div>{{timeTwo}} ----> {{timeTwo | format('YYYY-MM-DD 星期w')}}</div>
        <div>{{timeOne}} ----> {{timeTwo | format('YYYY年MM月DD日 星期w')}}</div>
        <div>时间戳:{{timeThree}} ----> {{timeThree | format('YYYY')}}</div>
        <div>时间戳:{{timeThree}} ----> {{timeThree | format('YYYY-MM')}}</div>
        <div>时间戳:{{timeThree}} ----> {{timeThree | format('YYYY-MM-DD HH:mm')}}</div>
        <div>时间戳:{{timeThree}} ----> {{timeThree | format('YYYY年MM月DD日')}}</div>
        <div>时间戳:{{timeThree}} ----> {{timeThree | format('YYYY年MM月DD日 星期w')}}</div>
        <div>时间戳:{{timeThree}} ----> {{timeThree | format('YYYY年MM月DD日 HH时mm分')}}</div>
    </div>
</body>
</html>
<script src="vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            timeOne: '2021/07/17 14:20:00',
            timeTwo: '2021-07-17 14:20:50',
            timeThree: 1626500149311 // 时间戳必须是数字类型
        },
        filters: {
            /**
             *格式化时间文本
             * @param {Date} time 时间
             * @param {String} format 文本格式
             *  Y 表示年份
             *  M 表示月份
             *  D 表示某日
             *  h 表示小时
             *  m 表示分
             *  s 表示秒
             *  w 表示星期几
             */
            format(time, format = 'YYYY-MM-DD HH:mm:ss') {
                let date = new Date(time)
                let o = {
                    'M+': date.getMonth() + 1,
                    'D+': date.getDate(),
                    'H+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds(),
                    'w': '日一二三四五六'.charAt(date.getDay())
                };

                format = format.replace(/Y{4}/, date.getFullYear()).replace(/Y{2}/, date.getFullYear().toString().substring(2));

                let k, reg
                for (k in o) {
                    reg = new RegExp(k);
                    /* eslint no-use-before-define:0 */
                    format = format.replace(reg, match);
                }
                function match(m) {
                    return m.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length);
                }

                return format;
            }
        }
    })
</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嫣嫣细语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值