Created
October 2, 2020 12:24
-
-
Save zachgoll/4bde24a2537c69df74b933efd4bc7cac to your computer and use it in GitHub Desktop.
Correct 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>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