Created
October 2, 2020 13:20
-
-
Save zachgoll/cc7269e99797085204aa8881fadc362b to your computer and use it in GitHub Desktop.
Asking user timezone
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
<html> | |
<head> | |
<style> | |
body { | |
padding: 20px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Date example web app</h2> | |
<h4>What is your birthday?</h4> | |
<input id="input-date" type="date" name="input-date" /> | |
<h5>Your birthday is...</h5> | |
<p id="string-val"></p> | |
<script> | |
const dateInput = document.querySelector("#input-date"); | |
const onDateChange = () => { | |
const dateValue = dateInput.value; | |
const userSpecifiedTimezone = "EDT"; | |
const dateParts = dateValue.split("-"); | |
const year = dateParts[0]; | |
const month = dateParts[1]; | |
const day = dateParts[2]; | |
const dateWithTimezone = new Date( | |
year + "-" + month + "-" + day + " (" + userSpecifiedTimezone + ")" | |
); | |
const convertedYear = dateWithTimezone.getFullYear(); | |
const convertedMonth = dateWithTimezone.getMonth() + 1; // Month is zero-indexed | |
const convertedDay = dateWithTimezone.getDate(); | |
document.querySelector("#string-val").innerHTML = | |
convertedYear + | |
"-" + | |
convertedMonth + | |
"-" + | |
convertedDay + | |
" (year-month-day)"; | |
}; | |
document | |
.querySelector('input[type="date"]') | |
.addEventListener("change", onDateChange); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment