screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Days Between Two Dates</title> </head> <body> <h1 class="title">Javascript Calculate Days Between Two Dates</h1> <label for="date1" class="input-label">Enter the first date:</label> <input type="date" id="date1" class="date-input"> <label for="date2" class="input-label">Enter the second date:</label> <input type="date" id="date2" class="date-input"> <button onclick="calculateDays()" class="calculate-button">Calculate</button> <p id="result"></p> <script> function calculateDays() { const date1 = document.getElementById('date1').value; const date2 = document.getElementById('date2').value; const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds const firstDate = new Date(date1); const secondDate = new Date(date2); const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay)); document.getElementById('result').textContent = `The number of days between ${date1} and ${date2} is: ${diffDays} days`; } </script> <style> body { width: 50%; margin: 0 auto } .title { text-align: center; color: #333; } .input-label { display: block; margin-top: 10px; font-weight: bold; } .date-input { width: 100%; padding: 8px; margin-top: 5px; margin-bottom: 15px; border: 1px solid #ccc; border-radius: 5px; box-sizing: border-box; } .calculate-button { background-color: #4caf50; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } .calculate-button:hover { background-color: #45a049; } </style> </body> </html>