js获取utc时间

    utc时间:<div id="id"></div> </br></br>

      let d = new Date();
      console.log(d.toUTCString());

  function formatDate(t) {
      return t <= 9 ? "0" + t : t;
    }
    function dateTiem() {
      let date = new Date();
      date.toUTCString();
      let month = (date.getMonth() + 1).toString().padStart(2, "0");
      let strDate = date.getDate().toString().padStart(2, "0");
      document.getElementById(
        "id"
      ).innerText = `${date.getFullYear()} - ${month} - ${strDate} ${date.getHours()} : ${this.formatDate(
        date.getMinutes()
      )} : ${this.formatDate(date.getSeconds())}`;
      setTimeout(() => {
        this.dateTiem();
      }, 1000);
    }

    this.dateTiem();
使用
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<body>
  <div id="app">

    utc时间:<div>{{time}}</div>
  </div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
  new Vue({
    el: '#app',
    data: function () {
      return {
        time: null,
      }
    },
    created() {
      setInterval(() => {
      // 假设定时调用接口获取时间
      console.log('定时');
      this.getDate();
    }, 5000);

    setInterval(() => {
      if(this.time) {
        this.dateTiem()
      }
      }, 1000);

    },
    methods: {

      getDate() {
        this.time = '2020-11-25 11:11:11';
      },

      
      formatDate(t) {
      return t <= 9 ? "0" + t : t;
    },

    dateTiem() {
      let d = (new Date(this.time).getTime()) + 1000;
      let date = new Date(d);
      console.log(date);
      let month = (date.getMonth() + 1).toString();
      let strDate = date.getDate().toString().padStart(2, "0");
    this.time = `${date.getFullYear()}-${month}-${strDate} ${date.getHours()}:${this.formatDate(
      date.getMinutes()
      )}:${this.formatDate(date.getSeconds())}`;
    }
    },
  })
</script>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值