fix(date utils fixed)

This commit is contained in:
2025-10-04 02:57:27 +05:30
parent 7920f40eb2
commit 14d2f3d7b8
2 changed files with 30 additions and 8 deletions

View File

@@ -360,7 +360,7 @@ export const PatientForm = forwardRef<PatientFormRef, PatientFormProps>(
<FormItem> <FormItem>
<FormLabel>Insurance ID</FormLabel> <FormLabel>Insurance ID</FormLabel>
<FormControl> <FormControl>
<Input {...field} value={field.value || ""} /> <Input {...field} value={String(field.value) || ""} />
</FormControl> </FormControl>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>

View File

@@ -35,14 +35,36 @@ export function parseLocalDate(input: string | Date): Date {
} }
/** /**
* Format a JS Date object as a `yyyy-MM-dd` string (in local time). * Strictly format to "YYYY-MM-DD" without timezone shifts.
* Useful for saving date-only data without time component. * - If input is already "YYYY-MM-DD", return it unchanged.
* - If input is a Date, use its local year/month/day directly (no TZ conversion).
* - If input is an ISO/timestamp string, first strip to "YYYY-MM-DD" safely.
*/ */
export function formatLocalDate(date: Date): string { export function formatLocalDate(input: string | Date): string {
const year = date.getFullYear(); // ← local time if (!input) return "";
const month = `${date.getMonth() + 1}`.padStart(2, "0");
const day = `${date.getDate()}`.padStart(2, "0"); // Case 1: already "YYYY-MM-DD"
return `${year}-${month}-${day}`; // e.g. "2025-07-15" if (typeof input === "string" && /^\d{4}-\d{2}-\d{2}$/.test(input)) {
return input;
}
// Case 2: Date object (use its local fields directly)
if (input instanceof Date) {
if (isNaN(input.getTime())) return "";
const year = input.getFullYear();
const month = `${input.getMonth() + 1}`.padStart(2, "0");
const day = `${input.getDate()}`.padStart(2, "0");
return `${year}-${month}-${day}`;
}
// Case 3: string with time/ISO — strip the "YYYY-MM-DD" part only
if (typeof input === "string") {
const parts = input.split("T");
const dateString = parts.length > 0 && parts[0] ? parts[0] : "";
return dateString;
}
return "";
} }
/** /**