<div class="container"> <h1>Date Range Picker</h1> <div class="input-group"> <div class="date-input"> <label for="startDate">Start Date</label> <input type="date" id="startDate"> </div> <div class="date-input"> <label for="endDate">End Date</label> <input type="date" id="endDate"> </div> </div> <div id="errorMessage" class="error-message" style="display: none;"></div> <div class="result" id="result" style="display: none;"> <div class="result-item"> <div class="result-label">Selected Range</div> <div class="result-value" id="dateRange">-</div> </div> <div class="result-item"> <div class="result-label">Total Days</div> <div class="result-value" id="totalDays">-</div> </div> </div> <button class="clear-btn" id="clearBtn">Clear Selection</button> </div>
* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; } .container { background: white; border-radius: 12px; padding: 32px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); max-width: 500px; width: 100%; } h1 { font-size: 24px; color: #2d3748; margin-bottom: 24px; text-align: center; } .input-group { display: flex; gap: 12px; margin-bottom: 20px; } .date-input { flex: 1; position: relative; } .date-input label { display: block; font-size: 13px; color: #4a5568; margin-bottom: 6px; font-weight: 500; } .date-input input { width: 100%; padding: 12px 16px; border: 2px solid #e2e8f0; border-radius: 8px; font-size: 15px; color: #2d3748; transition: all 0.2s; cursor: pointer; } .date-input input:hover { border-color: #cbd5e0; } .date-input input:focus { outline: none; border-color: #667eea; box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1); } .result { background: #f7fafc; border-radius: 8px; padding: 16px; margin-top: 20px; } .result-label { font-size: 13px; color: #718096; margin-bottom: 8px; font-weight: 500; } .result-value { font-size: 15px; color: #2d3748; font-weight: 600; } .result-item { margin-bottom: 12px; } .result-item:last-child { margin-bottom: 0; } .clear-btn { width: 100%; padding: 12px; background: #667eea; color: white; border: none; border-radius: 8px; font-size: 15px; font-weight: 600; cursor: pointer; transition: all 0.2s; margin-top: 16px; } .clear-btn:hover { background: #5568d3; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4); } .clear-btn:active { transform: translateY(0); } .error { border-color: #fc8181 !important; } .error-message { color: #e53e3e; font-size: 13px; margin-top: 6px; }
const startDateInput = document.getElementById('startDate'); const endDateInput = document.getElementById('endDate'); const resultDiv = document.getElementById('result'); const dateRangeSpan = document.getElementById('dateRange'); const totalDaysSpan = document.getElementById('totalDays'); const clearBtn = document.getElementById('clearBtn'); const errorMessage = document.getElementById('errorMessage'); function formatDate(date) { const options = { year: 'numeric', month: 'long', day: 'numeric' }; return date.toLocaleDateString('en-US', options); } function calculateDays(start, end) { const diff = Math.abs(end - start); return Math.ceil(diff / (1000 * 60 * 60 * 24)) + 1; } function validateAndUpdate() { const startVal = startDateInput.value; const endVal = endDateInput.value; errorMessage.style.display = 'none'; startDateInput.classList.remove('error'); endDateInput.classList.remove('error'); if (startVal && endVal) { const startDate = new Date(startVal); const endDate = new Date(endVal); if (startDate > endDate) { errorMessage.textContent = 'End date must be after start date'; errorMessage.style.display = 'block'; endDateInput.classList.add('error'); resultDiv.style.display = 'none'; return; } dateRangeSpan.textContent = `${formatDate(startDate)} - ${formatDate(endDate)}`; totalDaysSpan.textContent = `${calculateDays(startDate, endDate)} days`; resultDiv.style.display = 'block'; } else { resultDiv.style.display = 'none'; } } startDateInput.addEventListener('change', validateAndUpdate); endDateInput.addEventListener('change', validateAndUpdate); clearBtn.addEventListener('click', () => { startDateInput.value = ''; endDateInput.value = ''; resultDiv.style.display = 'none'; errorMessage.style.display = 'none'; startDateInput.classList.remove('error'); endDateInput.classList.remove('error'); });
Login to leave a comment
No comments yet. Be the first!
View Project
No comments yet. Be the first!