时间的格式化,获取格式化的时间与获取临近的时间(格式{yyyy}--{mm}--{dd})

以当前时间为例子,进行转化成标准格式:

 <div id="app">
      <div class="validate">
        <input type="text" v-model="value" />
        时间:<span>{{result}}</span><br />
        <br />
      </div>
    </div>
    <script src="vue.js"></script>
    <script>
      const vm = new Vue({
        el: '#app',
        data: {
          value: new Date()
        },
        computed: {
          result: function() {
            return this.getFormatDate(this.value);
          }
        },
        methods: {
          /**
          @param time 提供的时间
          @param needDay 日
          @return
          **/
          getFormatDate(time, needDay = true) {
            const formatObj = {
              y: time.getFullYear(),
              m:
                time.getMonth() + 1 < 10
                  ? '0' + (time.getMonth() + 1)
                  : time.getMonth() + 1,
              d: time.getDate() < 10 ? '0' + time.getDate() : time.getDate(),
              h: time.getHours(),
              i: time.getMinutes(),
              s: time.getSeconds(),
              a: time.getDay()
            };

            if (needDay) {
              return `${formatObj.y} - ${formatObj.m} - ${formatObj.d}`;
            } else {
              return `${formatObj.y} - ${formatObj.m}`;
            }
          }
        }
      });
    </script>

效果图:


版权声明:本文为gebieren原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。