From c85b64dac56d36f4a9a30743895cbf57a7d0990d Mon Sep 17 00:00:00 2001 From: Yashgabani845 Date: Thu, 30 May 2024 19:49:23 +0530 Subject: [PATCH] age calculator added --- Age Calculator/index.html | 56 ++++++++++++++++++++++++++++++++++++ Age Calculator/manifest.json | 13 +++++++++ Age Calculator/script.js | 56 ++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 Age Calculator/index.html create mode 100644 Age Calculator/manifest.json create mode 100644 Age Calculator/script.js diff --git a/Age Calculator/index.html b/Age Calculator/index.html new file mode 100644 index 00000000..00e248d1 --- /dev/null +++ b/Age Calculator/index.html @@ -0,0 +1,56 @@ + + + + + + + Date and Age Calculator + + +
+ + + +
+
+

Date Difference Calculator

+
+
+ + +
+
+ + +
+ +
+ +
+
+ +
+ + +
+
+

Age Calculator

+
+
+ + +
+ +
+ +
+
+
+ + + + diff --git a/Age Calculator/manifest.json b/Age Calculator/manifest.json new file mode 100644 index 00000000..fd262c30 --- /dev/null +++ b/Age Calculator/manifest.json @@ -0,0 +1,13 @@ +{ + "manifest_version": 3, + "name": "Age Calculator Extension", + "version": "1.0", + "description": "A simple age calculator Chrome extension", + "action": { + "default_popup": "index.html" + }, + "permissions": [ + "storage" + ] + } + \ No newline at end of file diff --git a/Age Calculator/script.js b/Age Calculator/script.js new file mode 100644 index 00000000..c3fc665b --- /dev/null +++ b/Age Calculator/script.js @@ -0,0 +1,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'; +});