Last active
October 2, 2020 13:44
-
-
Save zachgoll/1c5effea6b912af5ff5bb9778581306a to your computer and use it in GitHub Desktop.
Asking user timezone
This file contains hidden or 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
| const dateValue = dateInput.value; // Notice that we are grabbing the raw value, not the date value | |
| const userSpecifiedTimezone = "EDT"; // Assume, this was selected by the user | |
| // The date value is now a string, so grab the pieces | |
| const dateParts = dateValue.split("-"); | |
| const year = dateParts[0]; | |
| const month = dateParts[1]; | |
| const day = dateParts[2]; | |
| // Construct a new JS date using the individual pieces and timezone specification | |
| const dateWithTimezone = new Date( | |
| year + "-" + month + "-" + day + " " + userSpecifiedTimezone | |
| ); | |
| // Use the 'localized' Date methods to get the new date values | |
| const convertedYear = dateWithTimezone.getFullYear(); | |
| const convertedMonth = dateWithTimezone.getMonth() + 1; // Month is zero-indexed | |
| const convertedDay = dateWithTimezone.getDate(); | |
| // Format it however you want | |
| document.querySelector("#string-val").innerHTML = | |
| convertedYear + | |
| "-" + | |
| convertedMonth + | |
| "-" + | |
| convertedDay + | |
| " (year-month-day)"; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment