Skip to content

Instantly share code, notes, and snippets.

@zachgoll
Last active October 2, 2020 12:42
Show Gist options
  • Save zachgoll/68b7f07fcadea396c8f5c4c0d8eaf834 to your computer and use it in GitHub Desktop.
Save zachgoll/68b7f07fcadea396c8f5c4c0d8eaf834 to your computer and use it in GitHub Desktop.
Incorrect JS Date example
<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