fix(datetime): graceful fallback during SSR when navigator is undefined
This commit is contained in:
parent
80e5e117c8
commit
79344eb311
1 changed files with 8 additions and 6 deletions
|
|
@ -2,6 +2,8 @@ import { formatDistanceToNow, isValid } from "date-fns";
|
||||||
|
|
||||||
type DateInput = Date | string | number | null | undefined;
|
type DateInput = Date | string | number | null | undefined;
|
||||||
|
|
||||||
|
const getLocales = () => (typeof navigator !== "undefined" ? navigator.languages : undefined);
|
||||||
|
|
||||||
function formatValidDate(date: DateInput, formatter: (date: Date) => string): string {
|
function formatValidDate(date: DateInput, formatter: (date: Date) => string): string {
|
||||||
if (!date) return "Never";
|
if (!date) return "Never";
|
||||||
|
|
||||||
|
|
@ -14,7 +16,7 @@ function formatValidDate(date: DateInput, formatter: (date: Date) => string): st
|
||||||
// 1/10/2026, 2:30 PM
|
// 1/10/2026, 2:30 PM
|
||||||
export function formatDateTime(date: DateInput): string {
|
export function formatDateTime(date: DateInput): string {
|
||||||
return formatValidDate(date, (validDate) =>
|
return formatValidDate(date, (validDate) =>
|
||||||
Intl.DateTimeFormat(navigator.languages, {
|
Intl.DateTimeFormat(getLocales(), {
|
||||||
month: "numeric",
|
month: "numeric",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
|
|
@ -27,7 +29,7 @@ export function formatDateTime(date: DateInput): string {
|
||||||
// Jan 10, 2026
|
// Jan 10, 2026
|
||||||
export function formatDateWithMonth(date: DateInput): string {
|
export function formatDateWithMonth(date: DateInput): string {
|
||||||
return formatValidDate(date, (validDate) =>
|
return formatValidDate(date, (validDate) =>
|
||||||
Intl.DateTimeFormat(navigator.languages, {
|
Intl.DateTimeFormat(getLocales(), {
|
||||||
month: "short",
|
month: "short",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
|
|
@ -38,7 +40,7 @@ export function formatDateWithMonth(date: DateInput): string {
|
||||||
// 1/10/2026
|
// 1/10/2026
|
||||||
export function formatDate(date: DateInput): string {
|
export function formatDate(date: DateInput): string {
|
||||||
return formatValidDate(date, (validDate) =>
|
return formatValidDate(date, (validDate) =>
|
||||||
Intl.DateTimeFormat(navigator.languages, {
|
Intl.DateTimeFormat(getLocales(), {
|
||||||
month: "numeric",
|
month: "numeric",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
|
|
@ -49,7 +51,7 @@ export function formatDate(date: DateInput): string {
|
||||||
// 1/10
|
// 1/10
|
||||||
export function formatShortDate(date: DateInput): string {
|
export function formatShortDate(date: DateInput): string {
|
||||||
return formatValidDate(date, (validDate) =>
|
return formatValidDate(date, (validDate) =>
|
||||||
Intl.DateTimeFormat(navigator.languages, {
|
Intl.DateTimeFormat(getLocales(), {
|
||||||
month: "numeric",
|
month: "numeric",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
}).format(validDate),
|
}).format(validDate),
|
||||||
|
|
@ -59,7 +61,7 @@ export function formatShortDate(date: DateInput): string {
|
||||||
// 1/10, 2:30 PM
|
// 1/10, 2:30 PM
|
||||||
export function formatShortDateTime(date: DateInput): string {
|
export function formatShortDateTime(date: DateInput): string {
|
||||||
return formatValidDate(date, (validDate) =>
|
return formatValidDate(date, (validDate) =>
|
||||||
Intl.DateTimeFormat(navigator.languages, {
|
Intl.DateTimeFormat(getLocales(), {
|
||||||
month: "numeric",
|
month: "numeric",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
hour: "numeric",
|
hour: "numeric",
|
||||||
|
|
@ -71,7 +73,7 @@ export function formatShortDateTime(date: DateInput): string {
|
||||||
// 2:30 PM
|
// 2:30 PM
|
||||||
export function formatTime(date: DateInput): string {
|
export function formatTime(date: DateInput): string {
|
||||||
return formatValidDate(date, (validDate) =>
|
return formatValidDate(date, (validDate) =>
|
||||||
Intl.DateTimeFormat(navigator.languages, {
|
Intl.DateTimeFormat(getLocales(), {
|
||||||
hour: "numeric",
|
hour: "numeric",
|
||||||
minute: "numeric",
|
minute: "numeric",
|
||||||
}).format(validDate),
|
}).format(validDate),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue