tools.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import moment from 'moment-timezone';
  2. import 'moment-timezone/data/packed/latest.json';
  3. import { useUserStore } from '@/stores/modules/user'
  4. const userStore = useUserStore()
  5. const formatString = computed(() => {
  6. return userStore.dateFormat || 'MM/DD/YYYY'
  7. })
  8. export const formatTimezone = (time: string, timezone?: string, is12HourClock?: boolean, originFormat?: string) => {
  9. if (!time) return '--'
  10. let formattedTime = ''
  11. if (time.length > 12) {
  12. if (is12HourClock) {
  13. // 如果是12小时制,使用12小时制格式化
  14. formattedTime = moment(time, originFormat).format(`${formatString.value} hh:mm A`)
  15. } else {
  16. // 如果是24小时制,使用24小时制格式化
  17. formattedTime = moment(time, originFormat).format(`${formatString.value} HH:mm`)
  18. }
  19. if (!timezone) {
  20. return formattedTime
  21. }
  22. let utcOffset = ''
  23. const timeZoneOffset = moment.tz(time, timezone).format('Z')
  24. // 替换 "+07:00" 为 "UTC+07"
  25. utcOffset = `(UTC${timeZoneOffset.slice(0, 3)})`
  26. return `${formattedTime} ${utcOffset}`
  27. } else {
  28. formattedTime = moment(time, originFormat).format(formatString.value)
  29. return formattedTime
  30. }
  31. }
  32. /**
  33. * 将服务器时间转换为用户时区时间
  34. * @param
  35. * @returns
  36. */
  37. export const formatTimezoneByUser = (time: string, timeFormate: string, showHour?: boolean) => {
  38. if (!time) return '--'
  39. let curFormatString = showHour ? formatString.value + ' HH:mm' : formatString.value
  40. return moment.tz(time, timeFormate, 'America/Los_Angeles').local().format(curFormatString)
  41. }
  42. /**
  43. * 返回传入地区的UTC时区格式化
  44. * @param timezone
  45. * @returns
  46. */
  47. export const getTimezone = (timezone: string, time?: string): string => {
  48. if (!timezone) return ''
  49. const computedTime = time ? time : moment(time).format(formatString.value)
  50. const offset = moment.tz(computedTime, timezone).format('Z')
  51. return `UTC${offset.slice(0, 3)}`
  52. }
  53. export const formatTimezoneByUTCorGMT = (time: string, timezone: string) => {
  54. if (!time) return '--'
  55. let formattedTime = ''
  56. formattedTime = moment(time).format(`${formatString.value} HH:mm`)
  57. let gmtOffset = ''
  58. if (timezone != null) {
  59. const timeZoneOffset = moment.tz(time, timezone).format('Z')
  60. // 替换 "+07:00" 为 "GMT+07"
  61. if (timezone.includes('Seoul')) {
  62. gmtOffset = `(UTC${timeZoneOffset.slice(0, 3)})`
  63. } else {
  64. gmtOffset = `(GMT${timeZoneOffset.slice(0, 3)})`
  65. }
  66. return `${formattedTime} ${gmtOffset}`
  67. }
  68. }
  69. /**
  70. * 判断是否是欧洲地区
  71. */
  72. export const isEuropean = () => {
  73. const userLanguage = navigator.language || 'en-US'
  74. const europeanLocales = ['de', 'fr', 'it', 'es', 'nl', 'pl'] // 例:德语、法语、西班牙语等
  75. return europeanLocales.some((locale) => userLanguage.startsWith(locale))
  76. }
  77. /**
  78. * 根据传入的地区 格式化数字
  79. * @param number - 需要格式化的数字
  80. * @param digits - 小数位数
  81. */
  82. export const formatNumber = (number: number, digits?: number): string => {
  83. if (isNaN(number)) return '0'
  84. const userLanguage = userStore.userInfo?.numbers_format === 'European' ? 'de-DE' : 'zh-CN'
  85. // 设置数字格式化的选项
  86. const options: Intl.NumberFormatOptions = {
  87. style: 'decimal'
  88. // minimumFractionDigits: digits
  89. // maximumFractionDigits: 3
  90. }
  91. digits ?? (options.minimumFractionDigits = digits)
  92. // 其他地区使用默认格式
  93. return new Intl.NumberFormat(userLanguage, options).format(number)
  94. }
  95. /**
  96. * 根据用户地区判断日期格式
  97. * @returns {string} - 返回日期格式
  98. */
  99. export const getDateFormat = () => {
  100. const userLanguage = navigator.language || 'en-US' // 获取浏览器的语言设置
  101. // 判断用户地区
  102. if (userLanguage === 'en-US') {
  103. return 'MM/DD/YYYY' // 美国使用 MM/DD/YYYY 格式
  104. } else if (
  105. userLanguage.startsWith('de') ||
  106. userLanguage.startsWith('fr') ||
  107. userLanguage.startsWith('it') ||
  108. userLanguage.startsWith('es') ||
  109. userLanguage.startsWith('pl') ||
  110. userLanguage.startsWith('nl') ||
  111. userLanguage.startsWith('pt') ||
  112. userLanguage.startsWith('se')
  113. ) {
  114. return 'DD/MM/YYYY' // 其他欧洲国家(例:德语、法语、西班牙语等)使用 DD/MM/YYYY 格式
  115. } else if (['zh-CN', 'ja-JP', 'ko-KR'].includes(userLanguage)) {
  116. return 'YYYY-MM-DD' // 东亚国家(如简体中文、日语、韩语)使用 YYYY-MM-DD 格式
  117. } else {
  118. return 'DD/MM/YYYY' // 其他地区默认 DD/MM/YYYY 格式
  119. }
  120. }
  121. /**
  122. * 返回用户设备是否是Mac系统或者iPhone系统
  123. * @returns {boolean}
  124. */
  125. export const isMacOS = () => {
  126. const userAgent = navigator.userAgent
  127. if (userAgent.includes('iPhone') || userAgent.includes('iPad') || userAgent.includes('Mac')) {
  128. return true
  129. } else {
  130. return false
  131. }
  132. }