统一前端时间按酒店时区显示
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { getReservationTimeZone, reservationTimeZone } from '@/config/reservationConfig'
|
||||
|
||||
export function formatReservationDateTime(value: string | null | undefined): string {
|
||||
if (!value) {
|
||||
return '-'
|
||||
}
|
||||
return value.replace('T', ' ').replace(/\+.*$/, '')
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) {
|
||||
return value
|
||||
}
|
||||
return formatDateTimeInTimeZone(date, getReservationTimeZone())
|
||||
}
|
||||
|
||||
export function formatReservationDateRange(arrivalDate: string, departureDate: string): string {
|
||||
@@ -16,3 +22,26 @@ export function formatReservationCurrency(amount: number, currencyCode: string):
|
||||
maximumFractionDigits: 0,
|
||||
}).format(amount)
|
||||
}
|
||||
|
||||
function formatDateTimeInTimeZone(date: Date, timeZone: string): string {
|
||||
try {
|
||||
return formatDateTimeParts(date, timeZone)
|
||||
} catch {
|
||||
return formatDateTimeParts(date, reservationTimeZone)
|
||||
}
|
||||
}
|
||||
|
||||
function formatDateTimeParts(date: Date, timeZone: string): string {
|
||||
const parts = new Intl.DateTimeFormat('en-GB', {
|
||||
timeZone,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
hourCycle: 'h23',
|
||||
}).formatToParts(date)
|
||||
const valueByType = Object.fromEntries(parts.map((part) => [part.type, part.value]))
|
||||
return `${valueByType.year}-${valueByType.month}-${valueByType.day} ${valueByType.hour}:${valueByType.minute}:${valueByType.second}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user