RE: RE: JavaScript开发笔记
You are viewing a single comment's thread from:

RE: JavaScript开发笔记

Words
4
Reading
1 min
Listen
Play
7M

时区问题

 // 获取当前时间的时区
  const currentTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone

  // 判断时区是否在中国内地或港澳台地区
  if (currentTimeZone === 'Asia/Shanghai'
    || currentTimeZone === 'Asia/Chongqing'
    || currentTimeZone === 'Asia/Harbin'
    || currentTimeZone === 'Asia/Kashgar'
    || currentTimeZone === 'Asia/Urumqi'
    || currentTimeZone === 'Asia/Hong_Kong'
    || currentTimeZone === 'Asia/Taipei'
    || currentTimeZone === 'Asia/Macau') {
    return true
  }

//国际时间转为当地时间
//2023-05-29T03:01:07.545Z  -> 2023-5-29 11:01:07
function TimesToLocal() {
    let date1 = new Date()
    console.log(22, date1)
    return date1.toLocaleDateString().replace(/\//g, "-") + " " + date1.toLocaleTimeString()
}
let ee = TimesToLocal() 
console.log(55, ee)


//时间戳转变成当地时间
function TimestampToDate(Timestamp) {
    let date1 = new Date(Timestamp)
    return date1.toLocaleDateString().replace(/\//g, "-") + " " + date1.toLocaleTimeString()
    // return date1.toLocaleDateString().replace(/\//g, "-") + " " + date1.toTimeString().substr(0, 8)
}
let ee = TimestampToDate(1684201632279) 
console.log(55, ee)