| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import moment from 'moment-timezone'
- import { useUserStore } from '@/stores/modules/user'
- const userStore = useUserStore()
- const formatString = computed(() => {
- return userStore.dateFormat || 'MM/DD/YYYY'
- })
- export const formatTimezone = (time: string, timezone?: string, is12HourClock?: boolean) => {
- if (!time) return '--'
- let formattedTime = ''
- if (time.length > 12) {
- if (is12HourClock) {
- // 如果是12小时制,使用12小时制格式化
- formattedTime = moment(time).format(`${formatString.value} hh:mm A`)
- } else {
- // 如果是24小时制,使用24小时制格式化
- formattedTime = moment(time).format(`${formatString.value} HH:mm`)
- }
- if (!timezone) {
- return formattedTime
- }
- let utcOffset = ''
- const timeZoneOffset = moment.tz(time, timezone).format('Z')
- // 替换 "+07:00" 为 "UTC+07"
- utcOffset = `(UTC${timeZoneOffset.slice(0, 3)})`
- return `${formattedTime} ${utcOffset}`
- } else {
- formattedTime = moment(time).format(formatString.value)
- return formattedTime
- }
- }
- /**
- * 返回传入地区的UTC时区格式化
- * @param timezone
- * @returns
- */
- export const getTimezone = (timezone: string, time?: string): string => {
- if (!timezone) return ''
- const computedTime = time ? time : moment(time).format(formatString.value)
- const offset = moment.tz(computedTime, timezone).format('Z')
- return `UTC${offset.slice(0, 3)}`
- }
- export const formatTimezoneByUTCorGMT = (time: string, timezone: string) => {
- if (!time) return '--'
- let formattedTime = ''
- formattedTime = moment(time).format(`${formatString.value} HH:mm`)
- let gmtOffset = ''
- if (timezone != null) {
- const timeZoneOffset = moment.tz(time, timezone).format('Z')
- // 替换 "+07:00" 为 "GMT+07"
- if (timezone.includes('Seoul')) {
- gmtOffset = `(UTC${timeZoneOffset.slice(0, 3)})`
- } else {
- gmtOffset = `(GMT${timeZoneOffset.slice(0, 3)})`
- }
- return `${formattedTime} ${gmtOffset}`
- }
- }
- /**
- * 判断是否是欧洲地区
- */
- export const isEuropean = () => {
- const userLanguage = navigator.language || 'en-US'
- const europeanLocales = ['de', 'fr', 'it', 'es', 'nl', 'pl'] // 例:德语、法语、西班牙语等
- return europeanLocales.some((locale) => userLanguage.startsWith(locale))
- }
- /**
- * 根据传入的地区 格式化数字
- * @param number - 需要格式化的数字
- * @param digits - 小数位数
- */
- export const formatNumber = (number: number, digits?: number): string => {
- if (isNaN(number)) return '0'
- const userLanguage = userStore.userInfo?.numbers_format === 'European' ? 'de-DE' : 'zh-CN'
- // 设置数字格式化的选项
- const options: Intl.NumberFormatOptions = {
- style: 'decimal'
- // minimumFractionDigits: digits
- // maximumFractionDigits: 3
- }
- digits ?? (options.minimumFractionDigits = digits)
- // 其他地区使用默认格式
- return new Intl.NumberFormat(userLanguage, options).format(number)
- }
- /**
- * 根据用户地区判断日期格式
- * @returns {string} - 返回日期格式
- */
- export const getDateFormat = () => {
- const userLanguage = navigator.language || 'en-US' // 获取浏览器的语言设置
- // 判断用户地区
- if (userLanguage === 'en-US') {
- return 'MM/DD/YYYY' // 美国使用 MM/DD/YYYY 格式
- } else if (
- userLanguage.startsWith('de') ||
- userLanguage.startsWith('fr') ||
- userLanguage.startsWith('it') ||
- userLanguage.startsWith('es') ||
- userLanguage.startsWith('pl') ||
- userLanguage.startsWith('nl') ||
- userLanguage.startsWith('pt') ||
- userLanguage.startsWith('se')
- ) {
- return 'DD/MM/YYYY' // 其他欧洲国家(例:德语、法语、西班牙语等)使用 DD/MM/YYYY 格式
- } else if (['zh-CN', 'ja-JP', 'ko-KR'].includes(userLanguage)) {
- return 'YYYY-MM-DD' // 东亚国家(如简体中文、日语、韩语)使用 YYYY-MM-DD 格式
- } else {
- return 'DD/MM/YYYY' // 其他地区默认 DD/MM/YYYY 格式
- }
- }
- /**
- * 返回用户设备是否是Mac系统或者iPhone系统
- * @returns {boolean}
- */
- export const isMacOS = () => {
- const userAgent = navigator.userAgent
- if (userAgent.includes('iPhone') || userAgent.includes('iPad') || userAgent.includes('Mac')) {
- return true
- } else {
- return false
- }
- }
|