forked from Sulagna-Dutta-Roy/GGExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (44 loc) · 1.79 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
document.getElementById('dateForm').addEventListener('submit', function(e) {
e.preventDefault();
const startDate = new Date(document.getElementById('startDate').value);
const endDate = new Date(document.getElementById('endDate').value);
if (startDate > endDate) {
alert("End date should be greater than start date.");
return;
}
let years = endDate.getFullYear() - startDate.getFullYear();
let months = endDate.getMonth() - startDate.getMonth();
let days = endDate.getDate() - startDate.getDate();
if (days < 0) {
months--;
days += new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
document.getElementById('years').textContent = years;
document.getElementById('months').textContent = months;
document.getElementById('days').textContent = days;
document.getElementById('dateResult').style.display = 'block';
});
document.getElementById('ageForm').addEventListener('submit', function(e) {
e.preventDefault();
const birthdate = new Date(document.getElementById('birthdate').value);
const today = new Date();
let years = today.getFullYear() - birthdate.getFullYear();
let months = today.getMonth() - birthdate.getMonth();
let days = today.getDate() - birthdate.getDate();
if (days < 0) {
months--;
days += new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
document.getElementById('ageYears').textContent = years;
document.getElementById('ageMonths').textContent = months;
document.getElementById('ageDays').textContent = days;
document.getElementById('ageResult').style.display = 'block';
});