qudoco-fe/utils/format-date.ts

15 lines
498 B
TypeScript

/** Safe for Server Components — keep separate from `global.tsx` (`"use client"`). */
export function formatDate(date: Date | string | null) {
if (!date) return "";
const parsedDate = typeof date === "string" ? new Date(date) : date;
if (isNaN(parsedDate.getTime())) return "";
const year = parsedDate.getFullYear();
const month = String(parsedDate.getMonth() + 1).padStart(2, "0");
const day = String(parsedDate.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}