Last active
October 2, 2020 12:42
-
-
Save zachgoll/68b7f07fcadea396c8f5c4c0d8eaf834 to your computer and use it in GitHub Desktop.
Incorrect JS Date example
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.valueAsDate; | |
const day = dateValue.getDate(); | |
const month = dateValue.getMonth() + 1; // Return Value is 0 indexed | |
const year = dateValue.getFullYear(); | |
document.querySelector("#string-val").innerHTML = | |
year + "-" + month + "-" + day + " (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