Exchange Rate Calculator with Date

Exchange Rate Calculator with Historical Data

This calculator allows you to convert amounts between currencies using historical exchange rates for a specific date. This can be useful for historical accounting, travel planning, or understanding past financial transactions.

USD EUR GBP JPY CAD USD EUR GBP JPY CAD
var exchangeRates = { "USD": { "EUR": { "2023-01-01": 0.93, "2023-01-02": 0.94 }, "GBP": { "2023-01-01": 0.81, "2023-01-02": 0.82 }, "JPY": { "2023-01-01": 130.50, "2023-01-02": 131.20 }, "CAD": { "2023-01-01": 1.36, "2023-01-02": 1.37 } }, "EUR": { "USD": { "2023-01-01": 1.07, "2023-01-02": 1.06 }, "GBP": { "2023-01-01": 0.87, "2023-01-02": 0.88 }, "JPY": { "2023-01-01": 139.80, "2023-01-02": 140.10 }, "CAD": { "2023-01-01": 1.46, "2023-01-02": 1.47 } }, "GBP": { "USD": { "2023-01-01": 1.23, "2023-01-02": 1.22 }, "EUR": { "2023-01-01": 1.14, "2023-01-02": 1.13 }, "JPY": { "2023-01-01": 160.20, "2023-01-02": 161.50 }, "CAD": { "2023-01-01": 1.68, "2023-01-02": 1.69 } }, "JPY": { "USD": { "2023-01-01": 0.0077, "2023-01-02": 0.0076 }, "EUR": { "2023-01-01": 0.0071, "2023-01-02": 0.0071 }, "GBP": { "2023-01-01": 0.0062, "2023-01-02": 0.0062 }, "CAD": { "2023-01-01": 0.010, "2023-01-02": 0.010 } }, "CAD": { "USD": { "2023-01-01": 0.73, "2023-01-02": 0.73 }, "EUR": { "2023-01-01": 0.68, "2023-01-02": 0.68 }, "GBP": { "2023-01-01": 0.59, "2023-01-02": 0.59 }, "JPY": { "2023-01-01": 98.50, "2023-01-02": 99.00 } } }; function calculateExchangeRate() { var amount = parseFloat(document.getElementById("amount").value); var baseCurrency = document.getElementById("baseCurrency").value; var targetCurrency = document.getElementById("targetCurrency").value; var date = document.getElementById("date").value; var resultElement = document.getElementById("result"); if (isNaN(amount)) { resultElement.innerHTML = "Please enter a valid amount."; return; } if (baseCurrency === targetCurrency) { resultElement.innerHTML = amount + " " + baseCurrency + " is equal to " + amount + " " + targetCurrency; return; } var rate = null; if (exchangeRates[baseCurrency] && exchangeRates[baseCurrency][targetCurrency] && exchangeRates[baseCurrency][targetCurrency][date]) { rate = exchangeRates[baseCurrency][targetCurrency][date]; } else { // Fallback to the nearest available date if exact date is not found, or a default // For simplicity, we'll just state it's not available for now. // In a real-world scenario, you'd fetch from an API or implement more complex date logic. resultElement.innerHTML = "Exchange rate for " + date + " is not available. Please try another date or currency."; return; } var convertedAmount = amount * rate; resultElement.innerHTML = amount + " " + baseCurrency + " is equal to " + convertedAmount.toFixed(2) + " " + targetCurrency + " (as of " + date + ")"; } .exchange-rate-calculator { font-family: sans-serif; border: 1px solid #ccc; padding: 20px; border-radius: 8px; max-width: 500px; margin: 20px auto; background-color: #f9f9f9; } .exchange-rate-calculator h2 { text-align: center; margin-bottom: 20px; color: #333; } .exchange-rate-calculator p { text-align: justify; margin-bottom: 20px; color: #555; } .exchange-rate-calculator .input-section { display: grid; grid-template-columns: repeat(2, 1fr); gap: 15px; align-items: center; } .exchange-rate-calculator label { font-weight: bold; color: #444; } .exchange-rate-calculator input[type="number"], .exchange-rate-calculator input[type="date"], .exchange-rate-calculator select { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; } .exchange-rate-calculator button { grid-column: 1 / -1; padding: 12px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; } .exchange-rate-calculator button:hover { background-color: #0056b3; } #result { margin-top: 25px; padding: 15px; background-color: #e9ecef; border: 1px solid #ced4da; border-radius: 4px; text-align: center; font-size: 18px; color: #333; }

Leave a Comment