vue写一个yyyy-MM-dd HH:mm:ss的样式的自动变化的时钟

在这里插入图片描述

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
</head>

<body>
    <div id="app">
        {{ date | formateDate}}
    </div>
    <script type="text/javascript">
        var padDate = function (value) {

            return value < 10 ? '0' + value : value;

        };
        var app = new Vue({
            el: '#app',
            data: {
                date: new Date()
            },
            filters: {
                formateDate: function (value) {
                    var date = new Date(value);
                    var year = date.getFullYear();
                    var month = padDate(date.getMonth() + 1);
                    var day = padDate(date.getDate());
                    var hours = padDate(date.getHours());
                    var minutes = padDate(date.getMinutes());
                    var seconds = padDate(date.getSeconds());

                    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
                }
            },            
            created: function () {
            },
            mounted: function () {
                var _this = this;
                this.timer = setInterval(function () {

                    _this.date = new Date();

                }, 1000)
            },
            beforeDestroy: function () {
                if (this.timer) {
                    clearInterval(this.timer)
                }
            }
        })
    </script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/zwy15841139493/article/details/89307499