Generate a single index.html file that implements a fully functional date range picker…
- User
Generate a single index.html file that implements a fully functional date range picker component.
The component should display a calendar interface allowing users to:
Navigate through months and years using clear controls (e.g., "Previous", "Next"). Select a start date by clicking on a date cell. Select an end date after the start date by clicking on another date cell. Visually highlight the entire range of dates selected, from the start date to the end date, including the start and end dates themselves. Reset the selection if a new date is clicked that is before the currently selected start date, starting a new range selection from that new date. All necessary HTML structure, CSS styling, and JavaScript logic must be contained within this single index.html file. Avoid linking external stylesheets or scripts; embed everything directly. The goal is a self-contained, interactive date range selector ready to be opened directly in a browser.
Assistant
Date Range Picker Component
Here's a fully functional date range picker component implemented in a single HTML file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Range Picker</title> <style> * { box-sizing: border-box; font-family: Arial, sans-serif; } body { display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #f5f5f5; } .date-range-picker { width: 320px; background-color: white; border-radius: 8px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); overflow: hidden; } .header { display: flex; justify-content: space-between; align-items: center; padding: 15px; background-color: #4a6fa5; color: white; } .month-year { font-size: 18px; font-weight: bold; } .nav-button { background: none; border: none; color: white; font-size: 16px; cursor: pointer; padding: 5px 10px; border-radius: 4px; } .nav-button:hover { background-color: rgba(255, 255, 255, 0.2); } .calendar { padding: 10px; } .weekdays { display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; font-weight: bold; margin-bottom: 10px; } .weekday { padding: 8px; font-size: 14px; color: #666; } .days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; } .day { position: relative; height: 40px; display: flex; justify-content: center; align-items: center; cursor: pointer; border-radius: 50%; font-size: 14px; } .day:hover:not(.empty):not(.selected-start):not(.selected-end) { background-color: #f0f0f0; } .empty { cursor: default; } .selected-start, .selected-end { background-color: #4a6fa5; color: white; font-weight: bold; z-index: 2; } .in-range { background-color: rgba(74, 111, 165, 0.2); } .footer { padding: 15px; display: flex; justify-content: space-between; border-top: 1px solid #eee; } .selected-dates { font-size: 14px; color: #666; } .clear-button { background: none; border: none; color: #4a6fa5; cursor: pointer; font-size: 14px; padding: 0; } .clear-button:hover { text-decoration: underline; } </style> </head> <body> <div class="date-range-picker"> <div class="header"> <button class="nav-button prev-month">←</button> <div class="month-year">January 2023</div> <button class="nav-button next-month">→</button> </div> <div class="calendar"> <div class="weekdays"> <div class="weekday">Su</div> <div class="weekday">Mo</div> <div class="weekday">Tu</div> <div class="weekday">We</div> <div class="weekday">Th</div> <div class="weekday">Fr</div> <div class="weekday">Sa</div> </div> <div class="days"> <!-- Days will be generated by JavaScript --> </div> </div> <div class="footer"> <div class="selected-dates">Select a date range</div> <button class="clear-button">Clear</button> </div> </div> <script> document.addEventListener('DOMContentLoaded', function() { const dateRangePicker = { currentDate: new Date(), startDate: null, endDate: null, elements: { monthYear: document.querySelector('.month-year'), prevButton: document.querySelector('.prev-month'), nextButton: document.querySelector('.next-month'), daysContainer: document.querySelector('.days'), selectedDates: document.querySelector('.selected-dates'), clearButton: document.querySelector('.clear-button') }, init: function() { this.renderCalendar(); this.attachEventListeners(); }, attachEventListeners: function() { this.elements.prevButton.addEventListener('click', () => { this.currentDate.setMonth(this.currentDate.getMonth() - 1); this.renderCalendar(); }); this.elements.nextButton.addEventListener('click', () => { this.currentDate.setMonth(this.currentDate.getMonth() + 1); this.renderCalendar(); }); this.elements.clearButton.addEventListener('click', () => { this.startDate = null; this.endDate = null; this.updateSelectedDatesText(); this.renderCalendar(); }); }, renderCalendar: function() { const year = this.currentDate.getFullYear(); const month = this.currentDate.getMonth(); // Update month and year display const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; this.elements.monthYear.textContent = `${monthNames[month]} ${year}`; // Clear previous days this.elements.daysContainer.innerHTML = ''; // Get first day of month and total days in month const firstDay = new Date(year, month, 1).getDay(); const daysInMonth = new Date(year, month + 1, 0).getDate(); // Add empty cells for days before the first day of the month for (let i = 0; i < firstDay; i++) { const emptyDay = document.createElement('div'); emptyDay.className = 'day empty'; this.elements.daysContainer.appendChild(emptyDay); } // Add days of the month for (let day = 1; day <= daysInMonth; day++) { const dayElement = document.createElement('div'); dayElement.className = 'day'; dayElement.textContent = day; const currentDateObj = new Date(year, month, day); // Check if this day is the start date, end date, or in the selected range if (this.startDate && this.isSameDay(currentDateObj, this.startDate)) { dayElement.classList.add('selected-start'); } else if (this.endDate && this.isSameDay(currentDateObj, this.endDate)) { dayElement.classList.add('selected-end'); } else if (this.isInRange(currentDateObj)) { dayElement.classList.add('in-range'); } // Add click event to select dates dayElement.addEventListener('click', () => { this.handleDateSelection(new Date(year, month, day)); }); this.elements.daysContainer.appendChild(dayElement); } }, handleDateSelection: function(date) { if (!this.startDate || (this.startDate && this.endDate) || date < this.startDate) { // Start a new selection this.startDate = date; this.endDate = null; } else if (date > this.startDate) { // Complete the range this.endDate = date; } this.updateSelectedDatesText(); this.renderCalendar(); }, updateSelectedDatesText: function() { if (!this.startDate) { this.elements.selectedDates.textContent = 'Select a date range'; } else if (!this.endDate) { this.elements.selectedDates.textContent = `From: ${this.formatDate(this.startDate)}`; } else { this.elements.selectedDates.textContent = `${this.formatDate(this.startDate)} - ${this.formatDate(this.endDate)}`; } }, formatDate: function(date) { return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`; }, isSameDay: function(date1, date2) { return date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear(); }, isInRange: function(date) { if (!this.startDate || !this.endDate) return false; return date > this.startDate && date < this.endDate; } }; dateRangePicker.init(); }); </script> </body> </html>This implementation includes:
- A clean, responsive UI with a calendar display
- Month navigation controls (previous/next buttons)
- Date selection functionality:
- Click to select a start date
- Click again to select an end date
- Visual highlighting of the selected range
- Reset selection if a date before the current start date is clicked
- Clear button to reset the selection
- Display of the currently selected date range
The component is entirely self-contained in a single HTML file with embedded CSS and JavaScript.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.