Skip to content

Commit

Permalink
Merge pull request #26 from huzaifaazim0/main
Browse files Browse the repository at this point in the history
Add Next Birthday Countdown
  • Loading branch information
ShadBalti committed Feb 22, 2024
2 parents 624d91c + f28ede0 commit bc4e7ed
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
30 changes: 30 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<title> Age Calculator app</title>

<style>
#nextBirthdayDays,
#nextBirthdayHours,
#nextBirthdayMinutes,
#nextBirthdaySeconds {
font-family: Arial, sans-serif;
font-size: 18px;
font-weight: bold;
color: #333;
margin: 5px;
}

/* Optional: Add additional styling */
#nextBirthdayDays::before{
content: "Time until next birthday: ";
font-weight: normal;
color: #666;
}

.nextBirthdayResults{
display: none;
}
</style>


</head>

Expand All @@ -38,6 +62,12 @@ <h2>Age In</h2>
<p>Seconds: <span id="seconds"></span></p>
<p>Minutes: <span id="minutes"></span></p>
<p>Hours: <span id="hours"></span></p>
<div>
<p class="nextBirthdayResults"><span id="nextBirthdayDays"></span> Days
<span id="nextBirthdayHours"></span> Hours
<span id="nextBirthdayMinutes"></span> Minutes
<span id="nextBirthdaySeconds"></span> Seconds</p>
</div>
</section>
</main>
<section id="milestones">
Expand Down
26 changes: 25 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var countdownInterval;

function calculateAge() {
let userDate = document.getElementById('birthdate').value

Expand Down Expand Up @@ -63,6 +65,28 @@ function calculateAge() {
document.getElementById('age21').textContent = age21;
document.getElementById('age30').textContent = age30;
// Add more milestones as needed
};
if(countdownInterval){
clearInterval(countdownInterval)
}
countdownInterval = setInterval(() => calculateNextBirthday(birthdate), 1000);
document.getElementsByClassName('nextBirthdayResults')[0].style.display = 'block';
}

function calculateNextBirthday(birthdate) {
currentDate = new Date();
const nextBirthday = new Date(currentDate.getFullYear(), birthdate.getMonth(), birthdate.getDate());
if (nextBirthday < currentDate) {
nextBirthday.setFullYear(nextBirthday.getFullYear() + 1);
}
const timeUntilNextBirthday = nextBirthday - currentDate;

const daysUntilNextBirthday = Math.floor(timeUntilNextBirthday / (1000 * 60 * 60 * 24));
const hoursUntilNextBirthday = Math.floor((timeUntilNextBirthday % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutesUntilNextBirthday = Math.floor((timeUntilNextBirthday % (1000 * 60 * 60)) / (1000 * 60));
const secondsUntilNextBirthday = Math.floor((timeUntilNextBirthday % (1000 * 60)) / 1000);

document.getElementById('nextBirthdayDays').textContent = daysUntilNextBirthday;
document.getElementById('nextBirthdayHours').textContent = hoursUntilNextBirthday;
document.getElementById('nextBirthdayMinutes').textContent = minutesUntilNextBirthday;
document.getElementById('nextBirthdaySeconds').textContent = secondsUntilNextBirthday;
}

0 comments on commit bc4e7ed

Please sign in to comment.