Skip to content

Instantly share code, notes, and snippets.

@zachgoll
Created October 2, 2020 12:24
Show Gist options
  • Save zachgoll/4bde24a2537c69df74b933efd4bc7cac to your computer and use it in GitHub Desktop.
Save zachgoll/4bde24a2537c69df74b933efd4bc7cac to your computer and use it in GitHub Desktop.
Correct 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>Native Value (in milliseconds)</h5>
<p id="native-val"></p>
<h5>ISO Value</h5>
<p id="iso-val"></p>
<h5>String Value</h5>
<p id="string-val"></p>
<script>
const dateInput = document.querySelector("#input-date");
const onDateChange = () => {
const dateValue = dateInput.valueAsDate; // <--- THIS IS ALL THAT CHANGED
document.querySelector("#native-val").innerHTML = dateValue.valueOf();
document.querySelector("#iso-val").innerHTML = dateValue.toISOString();
document.querySelector("#string-val").innerHTML = dateValue.toString();
};
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