-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample13.html
67 lines (57 loc) · 2.33 KB
/
example13.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Determining a person’s age</title>
<style>
body {
margin: 1em;
font-size: 1.5em;
}
div {
margin-top: .5em;
}
</style>
</head>
<body>
<div>
<label for="date-picker1">Select the first date:</label>
<input type="date" id="date-picker1" value="2000-01-01">
</div>
<div>
<label for="date-picker2">Select the first date:</label>
<input type="date" id="date-picker2" value="2001-01-01">
</div>
<div id="output">
</div>
<script>
// Add a `change` event listener to each date picker
const datePicker1 = document.querySelector('#date-picker1');
datePicker1.addEventListener('change', handler);
const datePicker2 = document.querySelector('#date-picker2');
datePicker2.addEventListener('change', handler);
// Run this function when the date changes
function handler() {
// Get the dates from the date pickers
const date1 = new Date(datePicker1.value);
const date2 = new Date(datePicker2.value);
// Calculate the number of days between the dates
const daysDiff = daysBetween(date1, date2);
// Output the result
document.getElementById("output").innerHTML = `Difference: ${daysDiff} ${ daysDiff === 1 ? ' day' : ' days'} between the two dates.`;
}
function daysBetween(date1, date2) {
// Convert both dates to milliseconds
const date1Ms = date1.getTime();
const date2Ms = date2.getTime();
// Calculate the difference in milliseconds
const differenceMs = Math.abs(date1Ms - date2Ms);
// The number of milliseconds in one day
const ONEDAY = 1000 * 60 * 60 * 24;
// Convert to days and return
return Math.round(differenceMs/ONEDAY);
}
handler();
</script>
</body>
</html>