Membuat Kalkulator menghitung Umur

Membuat Kalkulator menghitung Umur

Berikut adalah contoh kode untuk Membuat Kalkulator menghitung Umur menggunakan HTML, CSS, dan JavaScript:

Kalkulator Umur

Kalkulator Umur

HTML
<!DOCTYPE html>
<html>
<head>
    <title>Kalkulator Umur</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
        }
        
        .container {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            text-align: center;
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 10px;
        }
        
        input[type="date"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        
        button {
            display: block;
            width: 100%;
            padding: 10px;
            margin-top: 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        
        #result {
            margin-top: 20px;
            text-align: center;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Kalkulator Umur</h1>
        <label for="birthdate">Tanggal Lahir:</label>
        <input type="date" id="birthdate">
        <button onclick="calculateAge()">Hitung</button>
        <div id="result"></div>
    </div>

    <script>
        function calculateAge() {
            var birthdate = document.getElementById("birthdate").value;
            var today = new Date();
            var birthdateObj = new Date(birthdate);
            var age = today.getFullYear() - birthdateObj.getFullYear();
            var monthDiff = today.getMonth() - birthdateObj.getMonth();
            
            if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthdateObj.getDate())) {
                age--;
            }
            
            document.getElementById("result").innerHTML = "Usia Anda adalah " + age + " tahun.";
        }
    </script>
</body>
</html>
HTML

Semoga bermanfaat

Leave a Reply

Your email address will not be published. Required fields are marked *