-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When using a <input type='date'> element, it is tricky to get the time zone correct when using the value, valueAsNumber, and valueAsDate fields. This is because the browser will always display the date as UTC, regardless of how you have set valueAsDate. Also, when getting the date out with new Date(input.value), where the value is yyyy-mm-dd, the Date parses as in UTC. When the user is interacting with the input element, they are thinking in local time. However, unless we offset the time that we set, it can show an incorrect date. This change sets the value date string manually to the local date. We could also offsets the Date that we assign to valueAsDate so that it will show a local date, or similarly with valueAsNumber, but this is harder to follow and we do not benefit from finer precision that simply 12am of the current day. When reading the selected date back into javascript, we also have to interpret it as a local date string. We do this by forcing the Date constructor to interpret the date as local by appending "T00:00" to the date string from input.value. We also must use this trick when reading the min and max date strings. We could instead use valueAsDate and manually shift in the opposite direction as when we set it, but this seems better.
- Loading branch information
Showing
3 changed files
with
32 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters