js 時間格式轉化 UTC yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 轉 new Date


(new Date).toISOString() 

// 獲取這種格式 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

//  2021-08-04T02:09:37.959Z"

 

var a = (new Date).toISOString()

var date =  new Date(a)

// 在解決時間格式

//  Wed Aug 04 2021 10:11:33 GMT+0800 (中國標准時間)

getCurrentDate(date ,2)

 

// 轉化方法

function getCurrentDate( date,format) {
//  var now = new Date()
  var now  =  date
  var year = now.getFullYear() // 得到年份
  var month = now.getMonth()// 得到月份
  var date = now.getDate()// 得到日期
  var hour = now.getHours()// 得到小時
  var minu = now.getMinutes()// 得到分鍾
  var sec = now.getSeconds()// 得到秒
  month = month + 1
  if (month < 10) month = '0' + month
  if (date < 10) date = '0' + date
  if (hour < 10) hour = '0' + hour
  if (minu < 10) minu = '0' + minu
  if (sec < 10) sec = '0' + sec
  var time = ''
  // 精確到天
  if (format === 1) {
    time = year + '-' + month + '-' + date
  } else if (format === 2) { // 精確到分
    time = year + '-' + month + '-' + date + ' ' + hour + ':' + minu + ':' + sec
  }
  return time
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM