js moment 时区互转

in 前端 with 0 comment

时区,互转, 默认使用 浏览器时区

import { getLocalUser } from './config'
import moment from 'moment-timezone'
import { getLanguage } from '@/plugins/languages'

export const _format_zh = 'YYYY-MM-DD HH:mm:ss'
export const _format_en = 'MMM DD,YYYY HH:mm:ss'
export const _format_short_en = 'MMM DD,YYYY'
export const _format_short_zh = 'YYYY-MM-DD'
export const _format_m = 'HH:mm a DD-MM-YYYY '

export const getBrowserTimezone = () => {
  return moment.tz.guess()
}

export const getFormat = () => {
  const language = getLanguage();
  if(language === 'zh-CN') {
    return _format_zh;
  }
  return _format_en
}

export const getShortFormat = () => {
  const language = getLanguage();
  if(language === 'zh-CN') {
    return _format_short_zh
  }
  return _format_short_en;
}

export const getCurrentTimeZone = () => {
  const user = getLocalUser()
  return user.timezone || getBrowserTimezone();
}
/**
 * 获取 moment UTC实例
 */
export const _moment_utc = date => {
  if (date) {
    return moment.tz(date, 'UTC')
  }
  return moment.tz('UTC')
}

/**
 * 获取 moment 当前时区实例
 */
export const _moment = date => {
  if (date) {
    return moment.tz(date, getCurrentTimeZone())
  }
  return moment.tz(getCurrentTimeZone())
}

/**
 * 将 UTC 时间转化为 当前时区的实例
 */
export const _moment_d = utc_date => {
  return _moment_utc(utc_date).tz(getCurrentTimeZone())
}

/**
 * 根据当前时区格式化时间
 * @param {*} time
 */

export const _format = (time, format) => {
  return _moment(time).format(format ? format : getFormat())
}

/**
 * 根据将UTC格式化成当前时区的时间
 * @param {*} time
 */
export const _format_utc = (utc_date, format) => {
  return _moment_d(utc_date).format(format ? format : getFormat())
}

/**
 * 根据将当前时区的时间 格式化成 UTC
 * @param {*} time
 */
export const _format_d = (date, format) => {
  return _moment(date).tz('UTC').format(format ? format :getFormat())
}

/**
 * 当前时间 | 当前时区
 * @param {*} format
 * @returns
 */
export const _now = (format) => {
  return _moment().format(format ? format : getFormat())
}

/**
 * 当前时间 | UTC
 * @param {*} format
 * @returns
 */
export const _now_utc = (format) => {
  return _moment_utc().format(format ? format : getFormat())
}

/**
 * 当前时间之差
 * @param {*} value
 * @returns
 */
export const _now_diff = utc_date => {
  // 将当前时区的时间转化为 UTC 时间
  const _now_unix = _moment().tz('UTC').unix()
  const _diff_nuix = _moment_utc(utc_date).unix()

  // console.log(`now: ${_now_unix} | diff: ${_diff_nuix}`);
  // console.log(`diff ${_diff_nuix - _now_unix}`);
  return _diff_nuix - _now_unix
}

/**
 * 当前时间之差 30分钟
 * @param {*} value
 * @returns
 */
export const _now_diff_30 = value => {
  return _now_diff(moment(value).add(30, 'minutes').format('YYYY-MM-DD HH:mm:ss'))
}

export const tz_difference = (timezone) => {
  // const utc = moment(moment.tz("America/Resolute").format('YYYY-MM-DD HH:mm:ss'))
  const utc = moment(moment.tz(timezone || moment.tz.guess()).format('YYYY-MM-DD HH:mm:ss'))
  const cur = moment(moment.tz('UTC').format('YYYY-MM-DD HH:mm:ss'))
  let _difference = utc.diff(cur, 'hours')
  console.log(_difference)
  // if (_difference > 0) {
  //   _difference = `+0${_difference}`
  // } else {
  //   _difference = _difference.toString()
  //   _difference = _difference.replace('-', '-0')
  // }
  if(_difference >= 0) {
    if(_difference < 10) {
      _difference = `0${_difference}`
    }
    _difference = '+' + _difference;
  }

  return `UTC${_difference}:00`
}

Comments are closed.