tools.ts 4.2 KB

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