Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

datetime local input disable hour in past

  1. User

    datetime local input disable hour in past

  2. Assistant

    To disable the selection of past hours in a local datetime input field, you can use JavaScript. Here's an example of how to implement it:

    HTML:

    <label for="datetime">Choose a date and time:</label>
    <input type="datetime-local" id="datetime" />
    

    JavaScript:

    const datetimeInput = document.getElementById('datetime');
    
    // Set min attribute to current date and time
    const currentDate = new Date().toISOString().split('.')[0];
    datetimeInput.setAttribute('min', currentDate);
    
    // Update min attribute if the date changes
    datetimeInput.addEventListener('input', () => {
      datetimeInput.setAttribute('min', datetimeInput.value);
    });
    

    In this example, the min attribute is set to the current date and time using the toISOString() method. This will disable the selection of past dates and hours. Additionally, if the user changes the date, the min attribute is updated dynamically to prevent selecting hours from an earlier date.

    Please note that this solution relies on JavaScript and may not be supported by older browsers.

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