js格式化 Thu Mar 07 2019 12:00:00 GMT+0800 (中國標準時間) 及相互轉化

1 Thu Mar 07 2019 12:00:00 GMT+0800 (中國標準時間) 轉換爲 2019-03-07 12:00:00
代碼如下

        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 (中國標準時間)
代碼如下

parserDate(date) {
      var t = Date.parse(date)
      if (!isNaN(t)) {
        return new Date(Date.parse(date.replace(/-/g, '/')))
      }
    },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章