Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 19 additions & 56 deletions src/TickerQ.Dashboard/wwwroot/src/utilities/dateTimeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading