Home  >  Article  >  Web Front-end  >  Building a Age Calculator Website

Building a Age Calculator Website

王林
王林Original
2024-08-10 06:44:03866browse

Building a Age Calculator Website

Introduction

Hello, fellow developers! Today, I'm excited to share a project I recently completed: an Age Calculator. This project allows users to calculate their exact age based on their date of birth, providing the result in a clear and user-friendly interface. It's a great way to practice working with JavaScript, particularly with date and time functions, while building something practical.

Project Overview

The Age Calculator is designed to give users an easy way to find out their current age in years, months, and days. Users simply enter their date of birth, and with the click of a button, their age is displayed. This project is perfect for developers who want to improve their skills in handling dates and building interactive web applications.

Features

  • Date Input: Users can enter their date of birth using a date picker for accurate input.
  • Age Calculation: The application calculates the user's exact age, down to the number of days.
  • Responsive Design: The tool is designed to be responsive, ensuring it works smoothly on different devices and screen sizes.

Technologies Used

  • HTML: Used for structuring the content on the webpage.
  • CSS: Applied for styling the webpage and ensuring it is responsive across various devices.
  • JavaScript: Implemented for handling the age calculation logic and updating the DOM.

Project Structure

Here's a quick look at the project structure:

Age-Calculator/
├── index.html
├── style.css
└── script.js
  • index.html: Contains the HTML structure of the webpage.
  • style.css: Holds the CSS styles, including responsive design rules.
  • script.js: Manages the dynamic aspects of the page using JavaScript.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Age-Calculator.git
    
  2. Open the project directory:

    cd Age-Calculator
    
  3. Run the project:

    • You can either run it on a local server or simply open the index.html file in a web browser.

Usage

  1. Open the website in a web browser.
  2. Enter your date of birth using the date picker input.
  3. Click the "Calculate Age" button to see your exact age displayed on the screen.

Code Explanation

HTML

The index.html file contains the structure of the webpage, including the input form and the section where the calculated age is displayed. Below is a snippet of the HTML code:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
93f0f5c25f18dab9d176bd4f6de5d30e
  1fc2df4564f5324148703df3b6ed50c1
  4f2fb0231f24e8aef524fc9bf9b9874f
  b2386ffb911b14667cb8f0f91ea547a7Age Calculator6e916e0f7d1e588d4f442bf645aedb2f
  097655f07349bae5bc1e68fdb79ed306
  468dcf7b0ee61aef03af1a1fbe6725fc
  5de102113aede4703971b3b780c58efb2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
  924ff17625d603f964501dd897c96cc6
    4a249f0d628e2318394fd9b75b4636b1Age Calculator473f0a7621bec819994bb5020d29372a
  16b28748ea4df4d9c2150843fecfba68

  4883ec0eb33c31828b7c767c806e14c7
    6aee8d1285fa83b7a99788e2e8734436
      2b836c77cbbab63c3f9e4b62c3ecc02aEnter your date of birth94b3e26ee717c64999d7867364b1b4a3
      53fcc26495096764d98f5e40e6cb0a78
      0daa849bc639fff17b006ca8cf3a07caCalculate Age65281c5ac262bf6d81768915a4a77ac0
      9369b95c0a54fa14078c71ff87b92a53Your age is 21 years old94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
  16b28748ea4df4d9c2150843fecfba68

  ffd6ba4147bda351239915f463e46e38
    e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
  16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

The style.css file contains styles that ensure the webpage is visually appealing and responsive. Here are some key styles:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Manrope", sans-serif;
  width: 100%;
  height: 100vh;
  background: #2962ff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
}

.header {
  margin-bottom: 80px;
  text-align: center;
}
.container {
  background: black;
  color: white;
  width: 600px;
  height: 300px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

.form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

p {
  font-weight: bold;
  margin: 20px;
}

input {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  width: 100%;
  max-width: 300px;
}

button {
  background-color: #007bff;
  color: white;
  border: none;
  margin: 20px;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #0062cc;
}

#result {
  margin-top: 20px;
  font-size: 24px;
  font-weight: bold;
}


.footer {
  margin: 70px;
  text-align: center;
}
.footer p{
  font-size: 14px;
  font-weight: 400;
}

JavaScript

The script.js file manages the age calculation logic and updates the result on the webpage. Below is a snippet of the JavaScript code:

const btnE1 = document.getElementById("btn");
const birthE1 = document.getElementById("birthday");
const resultE1 = document.getElementById("result");

function calculateAge() {
  const birthdayValue = birthE1.value;
  if (birthdayValue === "") {
    alert("Please enter your birthday");
  } else {
    const age = getAge(birthdayValue);
    resultE1.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old.`;
  }
}

function getAge(birthdayValue) {
  const birthdayDate = new Date(birthdayValue);
  const currentDate = new Date();
  let age = currentDate.getFullYear() - birthdayDate.getFullYear();
  const month = currentDate.getMonth() - birthdayDate.getMonth();

  if (
    month < 0 ||
    (month === 0 && currentDate.getDate() < birthdayDate.getDate())
  ) {
    age--;
  }

  return age;
}

btnE1.addEventListener("click", calculateAge);

Live Demo

You can check out the live demo of the Age Calculator here.

Conclusion

Building this Age Calculator was a rewarding experience that allowed me to deepen my understanding of working with dates and building interactive web applications. I hope you find this project useful and insightful for your own learning journey. Feel free to explore the code and adapt it to your needs. Happy coding!

Credits

This project was inspired by the need for a simple and effective age calculation tool.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Building a Age Calculator Website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn