Skip to content

Commit

Permalink
fix(dates): stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Jun 20, 2024
1 parent f6688b5 commit a4aa9f3
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/forms/src/components-control/input-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,31 @@ function validateByInputType(astro: AstroGlobal, aboutInput: AboutFormName, bind
}
}

function toDateTimeLocal(date: Date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');

return `${year}-${month}-${day}T${hours}:${minutes}`;
}


function stringifyDate(date?: Date | string, type?: ExtendedInputTypes) {
if (typeof date === 'string' || !date) {
return date;
}

switch (type) {
case 'date':
return date.toISOString().slice(0, 10);
return toDateTimeLocal(date).slice(0, 10);
case 'datetime-local':
return date.toISOString().slice(0, 16);
return toDateTimeLocal(date).slice(0, 16);
case 'time':
return date.toTimeString().slice(0, 5);
case 'month':
return date.toISOString().slice(0, 7);
return toDateTimeLocal(date).slice(0, 7);
case 'week':
return formatToDateWeek(date);
}
Expand Down

0 comments on commit a4aa9f3

Please sign in to comment.