CODE FOR TIME UNIT CONVERTOR

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Time Unit Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 100vh;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .converter {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            transition: transform 0.2s;
        }
        .converter:hover {
            transform: scale(1.02);
        }
        select, input[type="number"], button {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            background-color: #28a745;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #218838;
        }
        .result {
            margin-top: 15px;
            font-weight: bold;
        }
    </style>
</head>
<body>

<h1>Time Unit Converter</h1>
<div class="converter">
    <input type="number" id="timeInput" placeholder="Enter time" required>
    <select id="fromUnit">
        <option value="s">Seconds (s)</option>
        <option value="min">Minutes (min)</option>
        <option value="h">Hours (h)</option>
        <option value="d">Days (d)</option>
    </select>
    <select id="toUnit">
        <option value="s">Seconds (s)</option>
        <option value="min">Minutes (min)</option>
        <option value="h">Hours (h)</option>
        <option value="d">Days (d)</option>
    </select>
    <button onclick="convertTime()">Convert</button>
    <div class="result" id="result"></div>
</div>

<script>
    function convertTime() {
        const time = parseFloat(document.getElementById("timeInput").value);
        const fromUnit = document.getElementById("fromUnit").value;
        const toUnit = document.getElementById("toUnit").value;
        let result;

        if (isNaN(time)) {
            document.getElementById("result").innerText = "Please enter a valid time.";
            return;
        }

        // Conversion logic
        const conversions = {
            "s": {
                "min": time / 60,
                "h": time / 3600,
                "d": time / 86400,
            },
            "min": {
                "s": time * 60,
                "h": time / 60,
                "d": time / 1440,
            },
            "h": {
                "s": time * 3600,
                "min": time * 60,
                "d": time / 24,
            },
            "d": {
                "s": time * 86400,
                "min": time * 1440,
                "h": time * 24,
            }
        };

        result = conversions[fromUnit][toUnit];
        document.getElementById("result").innerText = `Result: ${result.toFixed(2)} ${toUnit}`;
    }
</script>

</body>
</html>
HTML

Leave a Reply

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

Resize text
Scroll to Top