Skip to content

Commit a4aa9f3

Browse files
committed
fix(dates): stringify
1 parent f6688b5 commit a4aa9f3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/forms/src/components-control/input-parse.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,31 @@ function validateByInputType(astro: AstroGlobal, aboutInput: AboutFormName, bind
118118
}
119119
}
120120

121+
function toDateTimeLocal(date: Date) {
122+
const year = date.getFullYear();
123+
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
124+
const day = String(date.getDate()).padStart(2, '0');
125+
const hours = String(date.getHours()).padStart(2, '0');
126+
const minutes = String(date.getMinutes()).padStart(2, '0');
127+
128+
return `${year}-${month}-${day}T${hours}:${minutes}`;
129+
}
130+
131+
121132
function stringifyDate(date?: Date | string, type?: ExtendedInputTypes) {
122133
if (typeof date === 'string' || !date) {
123134
return date;
124135
}
125136

126137
switch (type) {
127138
case 'date':
128-
return date.toISOString().slice(0, 10);
139+
return toDateTimeLocal(date).slice(0, 10);
129140
case 'datetime-local':
130-
return date.toISOString().slice(0, 16);
141+
return toDateTimeLocal(date).slice(0, 16);
131142
case 'time':
132143
return date.toTimeString().slice(0, 5);
133144
case 'month':
134-
return date.toISOString().slice(0, 7);
145+
return toDateTimeLocal(date).slice(0, 7);
135146
case 'week':
136147
return formatToDateWeek(date);
137148
}

0 commit comments

Comments
 (0)