-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: correctly format date conversion to a string for native date input #9076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Build successful! 🎉 |
| let dateValue = state.value == null ? '' : state.value.toString(); | ||
| let dateValue = ''; | ||
| if (state.value != null && 'toAbsoluteString' in state.value) { | ||
| dateValue = state.value.toAbsoluteString().replace('Z', ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there will never be something after the Z?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah there shouldn't. toAbsoluteString makes use of .toISOString() which formats the date as the following YYYY-MM-DDTHH:mm:ss.sssZ. see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
react-spectrum/packages/@internationalized/date/src/CalendarDate.ts
Lines 396 to 399 in f40b575
| /** Converts the date to an ISO 8601 formatted string in UTC. */ | |
| toAbsoluteString(): string { | |
| return this.toDate().toISOString(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think toAbsoluteString would not quite be right, because it returns the date in UTC whereas datetime-local expects it to be in the user's local time zone.
Also for granularity="day", the value may still be a CalendarDateTime or ZonedDateTime, we only display the date. Formatting it as a string will produce a full date time where the browser will expect only a date.
Maybe what we should do is always convert it to a CalendarDate or CalendarDateTime before converting to a string, depending on the granularity.
dateValue = state.granularity === 'day'
? toCalendarDate(state.value).toString()
: toCalendarDateTime('timeZone' in state.value ? toLocalTimeZone(state.value) : state.value).toString()|
Build successful! 🎉 |
Closes #9064
should be fine to get rid of the timezone since this only updates the value passed to the hidden date input and won't be submitted to a form. since neither
input type="date"orinput type="datetime-local"support timezones, it makes sense to remove it.✅ Pull Request Checklist:
📝 Test Instructions:
Go to the RAC DateField Autofill and DatePicker Autofill story. Check the browser to make sure there aren't any console warnings. If autofill is supported (so far have only gotten it to work in Safari), clear the date in the field and try autofilling with your contact info.
🧢 Your Project: