-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (55 loc) · 2.53 KB
/
script.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function calculateAge() {
// Check if both date fields are filled
var dobValue = document.getElementById("dob").value;
var ageDateValue = document.getElementById("ageDate").value;
if (!dobValue || !ageDateValue) {
alert("Please submit the dates.");
return;
}
const dob = new Date(dobValue);
const ageDate = new Date(ageDateValue);
// Check if ageDate is not earlier than dob
if (ageDate < dob) {
alert("Please select valid dates");
return;
}
// Check if dates are valid JavaScript dates
if (isNaN(dob) || isNaN(ageDate)) {
alert("Please enter valid dates.");
return;
}
var ageInMilliseconds = ageDate - dob;
var years = ageDate.getFullYear() - dob.getFullYear();
var months = ageDate.getMonth() - dob.getMonth();
var days = ageDate.getDate() - dob.getDate();
if (days < 0) {
months--;
days += new Date(ageDate.getFullYear(), ageDate.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
var result = "<h2>RESULT</h2><table><tr><th>Years</th><th>Months</th><th>Days</th></tr>";
result += "<tr><td>" + years + "</td><td>" + months + "</td><td>" + days + "</td></tr></table>";
var ageInDays = Math.floor(ageInMilliseconds / (24 * 60 * 60 * 1000));
var ageInHours = Math.floor(ageInMilliseconds / (60 * 60 * 1000));
var ageInMinutes = Math.floor(ageInMilliseconds / (60 * 1000));
var ageInSeconds = Math.floor(ageInMilliseconds / 1000);
var ageInMonths = (years * 12) + months;
result += "<h2>Exact Age in Different Units</h2>";
result += "<table><tr><th>Unit</th><th>Value</th></tr>";
result += "<tr><td>Month and Days</td><td>" + ageInMonths + " months " + days + " days</td></tr>";
if (ageInDays >= 7) {
var fullWeeks = Math.floor(ageInDays / 7);
var remainingDays = ageInDays % 7;
result += "<tr><td>Weeks</td><td>" + fullWeeks + " weeks and " + remainingDays + " days</td></tr>";
} else {
result += "<tr><td>Weeks</td><td>0 weeks and " + ageInDays + " days</td></tr>";
}
result += "<tr><td>Days</td><td>" + ageInDays + " days</td></tr>";
result += "<tr><td>Hours</td><td>" + ageInHours + " hours</td></tr>";
result += "<tr><td>Minutes</td><td>" + ageInMinutes + " minutes</td></tr>";
result += "<tr><td>Seconds</td><td>" + ageInSeconds + " seconds</td></tr></table>";
document.getElementById("result").innerHTML = result;
}