blog icon indicating copy to clipboard operation
blog copied to clipboard

前端国际化时间日期-如何强制设置时间为东八区

Open ly2011 opened this issue 6 years ago • 0 comments

import XDate from 'xdate'

// 时间戳转东八时区的'YYYY-MM'
function zhCNTimeFormat (time) {
  if (time) {
    // 1. new Date(time).getTimezoneOffset(): 求出格林威治时间和本地时间的时差,分钟为单位(例如,假如当前处于东一区, 此结果为 -1 * 60 = -60)
    // 2. offset: 求出当前时间(东八区与当前时区(东一区)的差值: 7小时)
    const offset = (8 * 60 + new Date(time).getTimezoneOffset()) * 60 * 1000 
    return new XDate(time + offset).toString('yyyy-MM')
  } else {
    return ''
  }
}

ly2011 avatar Jan 28 '19 01:01 ly2011