diff --git a/src/TickerQ.Dashboard/wwwroot/src/utilities/dateTimeParser.ts b/src/TickerQ.Dashboard/wwwroot/src/utilities/dateTimeParser.ts index 917bde4f..fe3a395b 100644 --- a/src/TickerQ.Dashboard/wwwroot/src/utilities/dateTimeParser.ts +++ b/src/TickerQ.Dashboard/wwwroot/src/utilities/dateTimeParser.ts @@ -6,69 +6,32 @@ export function formatDate( timeZone?: string ): string { if (!utcDateString) { - // nothing to format, return empty (or some placeholder) - return ''; - } - // 1) Ensure there’s a “Z” + return ‘’; + } + + // Ensure there’s a “Z” so JS treats it as UTC let iso = utcDateString.trim(); - if (!iso.endsWith('Z')) { - iso = iso.replace(' ', 'T') + 'Z'; + if (!iso.endsWith(‘Z’)) { + iso = iso.replace(‘ ‘, ‘T’) + ‘Z’; } - // 2) Now JS knows “that’s UTC” and will shift to local when you read getHours() - const dateObj = toTimeZoneDate(iso, timeZone); + const dateObj = new Date(iso); - // 3) Extract with local getters - const dd = String(dateObj.getDate()).padStart(2, '0'); - const MM = String(dateObj.getMonth() + 1).padStart(2, '0'); - const yyyy = dateObj.getFullYear(); + const options: Intl.DateTimeFormatOptions = { + year: ‘numeric’, + month: ‘2-digit’, + day: ‘2-digit’, + ...(timeZone ? { timeZone } : {}), + }; - if (!includeTime) { - return `${dd}.${MM}.${yyyy}`; + if (includeTime) { + options.hour = ‘2-digit’; + options.minute = ‘2-digit’; + options.second = ‘2-digit’; + options.hour12 = false; } - const hh = String(dateObj.getHours()).padStart(2, '0'); - const mm = String(dateObj.getMinutes()).padStart(2, '0'); - const ss = String(dateObj.getSeconds()).padStart(2, '0'); - - return `${dd}.${MM}.${yyyy} ${hh}:${mm}:${ss}`; -} - -function toTimeZoneDate(utcIsoString: string, timeZone?: string): Date { - const utcDate = new Date(utcIsoString); - - if (!timeZone) { - return utcDate; - } - - try { - // Use Intl API to get local parts in the target time zone - const fmt = new Intl.DateTimeFormat('en-US', { - timeZone, - year: 'numeric', - month: '2-digit', - day: '2-digit', - hour: '2-digit', - minute: '2-digit', - second: '2-digit', - hour12: false - }); - - const parts = fmt.formatToParts(utcDate); - const get = (type: string) => parts.find(p => p.type === type)?.value ?? '00'; - - const year = Number(get('year')); - const month = Number(get('month')) - 1; - const day = Number(get('day')); - const hour = Number(get('hour')); - const minute = Number(get('minute')); - const second = Number(get('second')); - - return new Date(year, month, day, hour, minute, second); - } catch { - // Fallback: return original UTC date if timeZone is invalid - return utcDate; - } + return new Intl.DateTimeFormat(undefined, options).format(dateObj); } export function formatTime(time: number, inputInMilliseconds = false): string {