zoukankan      html  css  js  c++  java
  • vue页面添加当前日期,并且格式化

    1.在vue页面定义一个div放置时间:

    <div>
     <h3 v-model="date" id="timeStyle">当前日期:{{date | formatDate}}</h3>
    </div>

    2.在mounted中定义定时器和定时器的销毁方法

      mounted() {
        let _this = this; // 声明一个变量指向Vue实例this,保证作用域一致
        this.timer = setInterval(() => {
          _this.date = new Date(); // 修改数据date
        }, 1000)
      },
      beforeDestroy() {
        if (this.timer) {
          clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器
        }
      },
      beforeDestroy: function () {
        if (this.timer) {
          clearInterval(this.timer);
        }
      },

    3.定义一个时间格式化的过滤器

    var padDate = function (value) {
    return value <10 ? '0' + value:value;
    };


    //时间过滤器 filters: { formatDate: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()); //var week = padDate(date.getDay()); let week = new Date(value).getDay(); let weeks = ["日","一","二","三","四","五","六"]; let getWeek = "星期" + weeks[week]; //return year + '-' + month + '-' + day +' '+'|' + ' ' + hours + ':' + minutes + ':' + seconds + ' ' +getWeek; return year + '-' + month + '-' + day +' '+'|'+' ' +getWeek; } },

    4.在style中定义时间显示的格式

    #timeStyle{
      color: #bcdcff;
      right: 180px;
      top: 20px;
      font-size: 16px;
      position: absolute;
    }

    5.时间显示效果

  • 相关阅读:
    js数据类型转换与字面量
    CSS精灵图与字体图标
    CSS元素的显示与隐藏
    .net core依赖注入学习
    WebAPI 参数问题
    .net EF code first 注意事项
    C#面向对象
    家用电信光纤内网IP改为公网IP
    页面JS引用添加随机参数避免页面缓存
    JS处理URL中的中文
  • 原文地址:https://www.cnblogs.com/liqinzhen/p/14214499.html
Copyright © 2011-2022 走看看