-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (50 loc) · 1.63 KB
/
app.js
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
window.onload = function () {
document.getElementById("date1").addEventListener("change", function() {
let input = this.value;
});
document.getElementById("date2").addEventListener("change", function() {
let input = this.value;
});
}
function handleForm() {
let date1 = document.getElementById("date1").value,
date2 = document.getElementById("date2").value,
fdate = new Date(date1).getTime(),
sdate = new Date(date2).getTime(),
days = formatNumbers((sdate - fdate) / 86400000),
hours = formatNumbers((sdate - fdate) / 3600000),
format = `${days} ${days > 1 ? 'days' : 'day'} or ${hours} hours`;
if (!date1 || !date2) {
results("Please use valid dates!", true);
} else if (fdate > sdate) {
results("First date must be older than second date!", true);
} else if (days == 0) {
results("That's the same day!", true);
} else {
results(format, false);
}
}
function formatNumbers(x){
if (x != 1) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
} else {
return x
}
}
function results(x, err) {
document.getElementById("ans").innerHTML = "";
if (err == true) {
let node = document.createElement("h2"),
format = document.createTextNode(x);
node.className = "error";
node.appendChild(format);
document.getElementById("ans").innerHTML = "";
document.getElementById("ans").appendChild(node);
} else {
let node = document.createElement("h2"),
format = document.createTextNode(x);
node.appendChild(format);
document.getElementById("ans").innerHTML = "";
document.getElementById("ans").appendChild(node);
}
}