Search code examples
htmldateinputtagsrestriction

Restrict date input for specific intervale of time


I am building a React App, and tho it has no connection with React, I wanna allow the user to pick only one day within the next two weeks with an input[type="date"] tag.

I don't know how to restrict the input only to a specific period of time (e.g. the next 14 days).

Please help me!


Solution

  • I am building a React App, and tho it has no connection with React

    What does this mean? You created a React project but aren't using React?

    There are several options to do this, react-datepicker and moment.js are my favorite for creating a date picker and creating custom date constraints.

    Here is a stackblitz of limiting a raw input to two weeks from now.

    let twoWeeks = new Date(Date.now() + 12096e5)
    .toISOString()
    .split('T')[0]
    .toLocaleString([], {
      year: 'numeric',
      month: 'numeric',
      day: 'numeric',
    });
    
    <input type="date" max={twoWeeks} />