tools.ts 3.4 KB

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