Skip to content
The Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

in a single html file give me a calendar component with range selection.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.0-flash-001’s answer.
  1. User

    in a single html file give me a calendar component with range selection.

  2. Assistant

    Below is an example of a single HTML file that implements a calendar component with a date‐range selection feature. You can save this file as (for example) calendar.html and open it in your browser.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Calendar with Range Selection</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          margin: 20px;
        }
        #calendar {
          max-width: 400px;
          margin: 0 auto;
          user-select: none;
        }
        .header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 10px;
        }
        .header button {
          padding: 5px 10px;
          font-size: 16px;
        }
        .header span {
          font-size: 18px;
          font-weight: bold;
        }
        .week-days, .days {
          display: grid;
          grid-template-columns: repeat(7, 1fr);
          text-align: center;
        }
        .week-days div {
          font-weight: bold;
          padding: 8px 0;
        }
        .days button {
          border: none;
          outline: none;
          background: none;
          padding: 8px 0;
          cursor: pointer;
          border-radius: 50%;
          transition: background 0.3s, color 0.3s;
        }
        .days button:hover {
          background: #eee;
        }
        .days button.disabled {
          color: #ccc;
          cursor: default;
        }
        .days button.selected {
          background: #4285F4;
          color: white;
        }
        .days button.in-range {
          background: #a9c1f7;
          color: white;
        }
      </style>
    </head>
    <body>
      <div id="calendar">
        <div class="header">
          <button id="prev">&laquo;</button>
          <span id="monthYear">Month Year</span>
          <button id="next">&raquo;</button>
        </div>
        <div class="week-days">
          <div>Sun</div>
          <div>Mon</div>
          <div>Tue</div>
          <div>Wed</div>
          <div>Thu</div>
          <div>Fri</div>
          <div>Sat</div>
        </div>
        <div class="days" id="days">
          <!-- Calendar days will be inserted here -->
        </div>
      </div>
    
      <script>
        // Global variables for the selected dates
        let selectedStart = null;
        let selectedEnd = null;
    
        // Current view month and year
        let currentDate = new Date();
        let currentMonth = currentDate.getMonth();
        let currentYear = currentDate.getFullYear();
    
        const monthYearEl = document.getElementById('monthYear');
        const daysEl = document.getElementById('days');
    
        // Render calendar for given month and year
        function renderCalendar(year, month) {
          // Clear previous days
          daysEl.innerHTML = '';
    
          // Set month title
          const monthNames = [
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
          ];
          monthYearEl.textContent = `${monthNames[month]} ${year}`;
          
          // Determine the first day of the month (0=Sunday, ...)
          const firstDay = new Date(year, month, 1).getDay();
          // Number of days in this month
          const daysInMonth = new Date(year, month + 1, 0).getDate();
          
          // Fill in blank days up to the first day of month (if needed)
          for (let i = 0; i < firstDay; i++) {
            const blankCell = document.createElement('div');
            blankCell.textContent = "";
            daysEl.appendChild(blankCell);
          }
          
          // Create a button for each day in the month
          for (let day = 1; day <= daysInMonth; day++) {
            const btn = document.createElement('button');
            btn.textContent = day;
            // build the date string (normalized) for comparison: YYYY-MM-DD
            const dateObj = new Date(year, month, day);
            btn.dataset.date = dateObj.toISOString().split('T')[0];
            
            // Add click event listener to select dates
            btn.addEventListener('click', function() {
              onDateClick(dateObj);
              updateDayStyles();
            });
            daysEl.appendChild(btn);
          }
        }
    
        // Handle day click for range selection
        function onDateClick(date) {
          // If no start selected or both start and end already set, restart selection.
          if (!selectedStart || (selectedStart && selectedEnd)) {
            selectedStart = date;
            selectedEnd = null;
          } else {
            // If the clicked day is before the start, restart by setting it as the new start.
            if (date < selectedStart) {
              selectedStart = date;
              selectedEnd = null;
            } else {
              selectedEnd = date;
            }
          }
        }
    
        // Update styling of day buttons to show selection and range
        function updateDayStyles() {
          // Get all day buttons within the calendar
          const buttons = daysEl.querySelectorAll('button');
          
          buttons.forEach(button => {
            button.classList.remove('selected', 'in-range');
            if (!button.dataset.date) return;
            const btnDate = new Date(button.dataset.date);
            
            // Check if this day is exactly the start or end
            if (selectedStart && isSameDate(btnDate, selectedStart)) {
              button.classList.add('selected');
            }
            if (selectedEnd && isSameDate(btnDate, selectedEnd)) {
              button.classList.add('selected');
            }
            
            // If a complete range is selected, add "in-range" style for dates in between
            if (selectedStart && selectedEnd) {
              if (btnDate > selectedStart && btnDate < selectedEnd) {
                button.classList.add('in-range');
              }
            }
          });
        }
    
        // Utility: check if two dates are the same ignoring time
        function isSameDate(d1, d2) {
          return d1.getFullYear() === d2.getFullYear() &&
                 d1.getMonth() === d2.getMonth() &&
                 d1.getDate() === d2.getDate();
        }
    
        // Navigation: previous month
        document.getElementById('prev').addEventListener('click', function() {
          selectedStart = null;
          selectedEnd = null;
          if (currentMonth === 0) {
            currentMonth = 11;
            currentYear--;
          } else {
            currentMonth--;
          }
          renderCalendar(currentYear, currentMonth);
        });
    
        // Navigation: next month
        document.getElementById('next').addEventListener('click', function() {
          selectedStart = null;
          selectedEnd = null;
          if (currentMonth === 11) {
            currentMonth = 0;
            currentYear++;
          } else {
            currentMonth++;
          }
          renderCalendar(currentYear, currentMonth);
        });
    
        // Initial render
        renderCalendar(currentYear, currentMonth);
      </script>
    </body>
    </html>
    

    How It Works

    1. Calendar Rendering:
      The renderCalendar function calculates the first day of the month (to know how many blank cells to leave) and the number of days in the month. It then dynamically creates a grid with the proper dates.

    2. Range Selection Logic:

      • Clicking on a day when no range is active or when a full range is already selected sets the clicked day as the start date.
      • If a start is already selected and the new clicked date is after it, then the new date becomes the end date.
      • If the new clicked date is before the start, then it resets the range by setting it as the new start.
    3. Styling:
      CSS classes (selected and in-range) highlight the chosen start and end dates as well as the dates in between.

    Feel free to modify the styling and logic to better suit your needs.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.