前端实现时区时间转换


```javascript
    //通过时区变时间
    handleFormChange(text, item) {
      clearInterval(this.timer);
      clearInterval(this.timerr);
      this.$axios.get("xxx").then((res) => {
        if (res.data.retCode === 0) {
          let con = res.data.info.curDateTime;
          console.log(con); //1609240448706
          let come = Number(con); // returns 23
          this.date = moment(come).format("YYYY-MM-DD HH:mm:ss");
          var time = moment(this.date).format();
          if (item == "right/US/Hawaii") {
            var timezone = -10; //目标时区 夏威夷
          } else if (item == "right/PST8PDT") {
            var timezone = -8; //目标时区 美国
          } else if (item == "right/UTC") {
            var timezone = 0; //目标时区 伦敦
          } else if (item == "right/Asia/Hong_Kong") {
            var timezone = 8; //目标时区 中国
          } else if (item == "right/Asia/Tokyo") {
            var timezone = 9; //目标时区 东京
          }
          var offset_GMT = new Date(time).getTimezoneOffset();
          var nowDate = new Date(time).getTime();
          var targetDate = new Date(
            nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000
          ); //当前的时间
          var current = targetDate.getTime(); //当前时区时间戳
          var curDateTime = moment(current).format("YYYY-MM-DD HH:mm:ss"); //转换为时分
          this.form.setFieldsValue({
            systemDate: curDateTime,
            EquipmentDate: curDateTime,
          });
        //动态展示(可以忽略)
          this.dateTime = curDateTime; //获取时间
          let newtime = moment(this.dateTime).format(); //转换成系统识别时间
          let currentS = new Date(newtime).getTime(); //转换为时间戳
          this.timerr = setInterval(() => {
            currentS = currentS + 1000; //给时间戳加1s
            //  将时间戳转换为时间格式
            this.curdateTime = moment(currentS).format("YYYY-MM-DD HH:mm:ss");
            this.form.setFieldsValue({
              EquipmentDate: this.curdateTime,
            });
          }, 1000);
        }
      });
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值