You've already forked domainstack.io
mirror of
https://github.com/jakejarvis/domainstack.io.git
synced 2025-12-02 19:33:48 -05:00
refactor: replace date formatting with native Intl.DateTimeFormat API for improved performance and accuracy
This commit is contained in:
@@ -1,25 +1,55 @@
|
||||
import { UTCDate } from "@date-fns/utc";
|
||||
import { format } from "date-fns";
|
||||
|
||||
export function formatDate(iso: string) {
|
||||
/**
|
||||
* Formats a date string in UTC using the native Intl.DateTimeFormat API.
|
||||
* @param iso - ISO 8601 date string or any valid date string
|
||||
* @returns Formatted date string (e.g., "Oct 2, 2025")
|
||||
*/
|
||||
export function formatDate(iso: string): string {
|
||||
try {
|
||||
const d = new Date(iso);
|
||||
if (Number.isNaN(d.getTime())) return iso;
|
||||
const utc = new UTCDate(d);
|
||||
// Example: Oct. 2, 2025
|
||||
return format(utc, "MMM. d, yyyy");
|
||||
|
||||
// Use Intl.DateTimeFormat for native, zero-bundle formatting
|
||||
// Output: "Oct 2, 2025"
|
||||
return new Intl.DateTimeFormat("en-US", {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
timeZone: "UTC",
|
||||
}).format(d);
|
||||
} catch {
|
||||
return iso;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatDateTimeUtc(iso: string) {
|
||||
/**
|
||||
* Formats a date string as ISO-like datetime in UTC using native Intl.DateTimeFormat API.
|
||||
* @param iso - ISO 8601 date string or any valid date string
|
||||
* @returns Formatted datetime string (e.g., "2025-10-02 14:30:05 UTC")
|
||||
*/
|
||||
export function formatDateTimeUtc(iso: string): string {
|
||||
try {
|
||||
const d = new Date(iso);
|
||||
if (Number.isNaN(d.getTime())) return iso;
|
||||
const utc = new UTCDate(d);
|
||||
// Example: 2025-10-02 14:30:05 UTC
|
||||
return format(utc, "yyyy-MM-dd HH:mm:ss 'UTC'");
|
||||
|
||||
// Use Intl.DateTimeFormat with formatToParts for precise control
|
||||
const formatter = new Intl.DateTimeFormat("en-US", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
hour12: false,
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
timeZone: "UTC",
|
||||
});
|
||||
|
||||
const parts = formatter.formatToParts(d);
|
||||
const partMap = Object.fromEntries(
|
||||
parts.map((p) => [p.type, p.value]),
|
||||
) as Record<string, string>;
|
||||
|
||||
// Construct: 2025-10-02 14:30:05 UTC
|
||||
return `${partMap.year}-${partMap.month}-${partMap.day} ${partMap.hour}:${partMap.minute}:${partMap.second} UTC`;
|
||||
} catch {
|
||||
return iso;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@bprogress/next": "^3.2.12",
|
||||
"@date-fns/utc": "^2.1.1",
|
||||
"@opentelemetry/api": "^1.9.0",
|
||||
"@posthog/nextjs-config": "^1.6.1",
|
||||
"@readme/http-status-codes": "^9.0.6",
|
||||
@@ -66,7 +65,7 @@
|
||||
"media-chrome": "^4.16.1",
|
||||
"motion": "^12.23.24",
|
||||
"ms": "3.0.0-canary.202508261828",
|
||||
"next": "16.0.5",
|
||||
"next": "16.0.6",
|
||||
"next-themes": "^0.4.6",
|
||||
"pg": "^8.16.3",
|
||||
"postgres": "^3.4.7",
|
||||
@@ -113,7 +112,7 @@
|
||||
"jsdom": "^27.2.0",
|
||||
"puppeteer": "24.26.1",
|
||||
"tailwindcss": "^4.1.17",
|
||||
"tsx": "^4.20.6",
|
||||
"tsx": "^4.21.0",
|
||||
"tw-animate-css": "^1.4.0",
|
||||
"typescript": "5.9.3",
|
||||
"vite-tsconfig-paths": "^5.1.4",
|
||||
|
||||
468
pnpm-lock.yaml
generated
468
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user