時間格式2020-09-29T18:02:02.000Z轉換爲「年月日時分秒「

遇到一個需求:
在這裏插入圖片描述
javascript

選擇時間後打印出來是 「2020-09-29T18:02:02.000Z」 格式的,後臺須要轉換爲 「2020-09-30 02:02:02」格式。前端

1、2020-09-29T18:02:02.000Zjava

T表示分隔符,Z表示的是UTC。web

UTC:世界標準時間,在世界標準時間上加上8小時,即東八區時間,也就是北京時間。npm

2、 2020-09-29T18:02:02.000Z轉換爲 2020-09-30 02:02:02 步驟:函數

①.引入 dayjs (一個輕量的處理時間和日期的javascript庫)this

  1. 下載 npm install dayjs --save
  2. main.js 中 全局引入
    import dayjs from ‘dayjs’
    Vue.prototype.dayjs = dayjs;

②.時間轉換函數spa

// 時間
    aaa() { 
      let time = '2020-09-29T18:02:02.000Z'
      time = this.formateDate(time);
      console.log(form); // 2020-09-30 2:2:2
    },
    
    // 時間格式轉換
    formateDate(time) { 
      // 使用dayjs 把世界標準時間轉換爲北京時間
      let date = this.dayjs(time).format();
      console.log(date) // 2020-09-30T02:02:02+08:00
      // 把2020-09-30T02:02:02+08:00 截取出 '2020-9-30 2:2:2'
      const arr = date.split("T");
      const d = arr[0];
      const darr = d.split("-");

      const t = arr[1];
      const tarr = t.split(".000");
      const marr = tarr[0].split(":");

      const dd =
        parseInt(darr[0]) +
        "-" +
        parseInt(darr[1]) +
        "-" +
        parseInt(darr[2]) +
        " " +
        parseInt(marr[0]) +
        ":" +
        parseInt(marr[1]) +
        ":" +
        parseInt(marr[2]);
      console.log(dd) // 2020-9-30 2:2:2
      return dd;
    },

web前端交流QQ羣:327814892prototype

十二星座的今日運勢,QQ掃碼查看星座運勢,還能領取現金紅包
在這裏插入圖片描述
code

相關文章
相關標籤/搜索