tools.ts 3.6 KB

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