Skip to content

Commit 5e58ccd

Browse files
committed
Fix for the DateTime ISO function
1 parent aacec56 commit 5e58ccd

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

apps/webapp/app/components/primitives/DateTime.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,52 @@ export function formatDateTime(
9898
}
9999

100100
export function formatDateTimeISO(date: Date, timeZone: string): string {
101-
return new Date(date.toLocaleString("en-US", { timeZone })).toISOString();
101+
// Special handling for UTC
102+
if (timeZone === "UTC") {
103+
return date.toISOString();
104+
}
105+
106+
// Get the offset in minutes for the specified timezone
107+
const formatter = new Intl.DateTimeFormat("en-US", {
108+
timeZone,
109+
timeZoneName: "shortOffset",
110+
});
111+
const tzOffset =
112+
formatter
113+
.formatToParts(date)
114+
.find((p) => p.type === "timeZoneName")
115+
?.value.replace("GMT", "") ?? "";
116+
117+
// Format the offset properly as ±HH:mm
118+
const offsetNum = parseInt(tzOffset);
119+
const offsetHours = Math.abs(Math.floor(offsetNum)).toString().padStart(2, "0");
120+
const sign = offsetNum >= 0 ? "+" : "-";
121+
const formattedOffset = `${sign}${offsetHours}:00`;
122+
123+
// Format the date parts
124+
const dateFormatter = new Intl.DateTimeFormat("en-US", {
125+
timeZone,
126+
year: "numeric",
127+
month: "2-digit",
128+
day: "2-digit",
129+
hour: "2-digit",
130+
minute: "2-digit",
131+
second: "2-digit",
132+
hour12: false,
133+
});
134+
135+
const parts = dateFormatter.formatToParts(date);
136+
const dateParts: Record<string, string> = {};
137+
parts.forEach((part) => {
138+
dateParts[part.type] = part.value;
139+
});
140+
141+
// Format: YYYY-MM-DDThh:mm:ss.sss±hh:mm
142+
const isoString =
143+
`${dateParts.year}-${dateParts.month}-${dateParts.day}T` +
144+
`${dateParts.hour}:${dateParts.minute}:${dateParts.second}.000${formattedOffset}`;
145+
146+
return isoString;
102147
}
103148

104149
// New component that only shows date when it changes

0 commit comments

Comments
 (0)