CodexBloom - Programming Q&A Platform

HTML <input type='date'> Not Formatting Correctly in Chrome on macOS - Timezone Issues

๐Ÿ‘€ Views: 9725 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-10
html input date timezone chrome HTML

I'm optimizing some code but I'm working with an scenario with the `<input type='date'>` element in my HTML form. When I select a date in Chrome on macOS, it seems to be outputting the date in UTC rather than the local timezone, which is causing problems when I try to submit the form. For example, if I select '2023-10-10' in the date picker, the value submitted is '2023-10-10T00:00:00Z'. Iโ€™ve tried using JavaScript to convert the date to local time before submission, but I need to seem to get it right. Hereโ€™s the code snippet Iโ€™m using: ```html <form id="myForm"> <label for="dateInput">Choose a date:</label> <input type="date" id="dateInput" name="dateInput"> <input type="submit" value="Submit"> </form> <script> document.getElementById('myForm').onsubmit = function() { const dateInput = document.getElementById('dateInput').value; const localDate = new Date(dateInput + 'T00:00:00'); console.log(localDate.toString()); // add code here to format and submit the date correctly }; </script> ``` Iโ€™ve also tried setting the timezone manually using `Intl.DateTimeFormat` but that didnโ€™t yield the expected results either. The date still appears in UTC on the server side. Is there a reliable way to ensure the date is submitted in the local timezone? Any insights or workarounds would be greatly appreciated! I've been using Html for about a year now.