const d = new Date(Thu Mar 07 2019 12:00:00 GMT+0800 (中國標准時間))
const resDate = d.getFullYear() + '-' + this.p((d.getMonth() + 1)) + '-' + this.p(d.getDate()) const resTime = this.p(d.getHours()) + ':' + this.p(d.getMinutes()) + ':' + this.p(d.getSeconds())

p為不夠10添加0的函數

p(s) {
      return s < 10 ? '0' + s : s
    },

2、2019-03-07 12:00:00轉換為 Thu Mar 07 2019 12:00:00 GMT+0800 (中國標准時間)

復制代碼
 1、 parserDate(date) {
      var t = Date.parse(date)
      if (!isNaN(t)) {
        return new Date(Date.parse(date.replace(/-/g, '/')))
      }
    },
 2、
     var a='2020-01-01';
      a = a.replace(/-/g,'/')
     var dateA = (new Date(a)).getTime()
 
復制代碼

3、將時間戳轉換成日期格式:

復制代碼
function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//時間戳為10位需*1000,時間戳為13位的話不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
    }
    timestampToTime(1403058804);
    console.log(timestampToTime(1403058804));//2014-06-18 10:33:24
復制代碼

4、將日期格式轉換成時間戳:

復制代碼
var date = new Date('2014-04-23 18:55:49:123');
    // 有三種方式獲取
    var time1 = date.getTime();
    var time2 = date.valueOf();
    var time3 = Date.parse(date);
    console.log(time1);//1398250549123
    console.log(time2);//1398250549123
    console.log(time3);//1398250549000
復制代碼

  以上三種獲取方式的區別:

  第一、第二種:會精確到毫秒

  第三種:只能精確到秒,毫秒用000替代

  以上三個輸出結果可觀察其區別

 
轉載自博客園:
作者:澤東玩乾坤
原文地址:https://www.cnblogs.com/mengzekun/p/12295265.html
聲明:本博文只用於知識分享與傳遞信息,如涉及作品內容、版權和其它問題,請在30日內與本人聯系,我將在第一時間刪除內容