How to disable past dates from today date with Material ui reactjs?

10,064

Solution 1

Here it is:

import React from 'react';
import DatePicker from 'material-ui/DatePicker';

function disablePrevDates(startDate) {
  const startSeconds = Date.parse(startDate);
  return (date) => {
    return Date.parse(date) < startSeconds;
  }
}

class App extends React.Component {
    
    render() {
        const startDate = new Date();
//      or:
//      const startDate = new Date('December 20, 2016');
//      const startDate = this.state.firstDate;
      
        return (
            <div>
                <DatePicker 
                  hintText="Check-in" 
                  shouldDisableDate={disablePrevDates(startDate)}
                />
            </div>
        )
    }
}

export default App;

Solution 2

related to the title of your question.

const minDate = new Date(Date.now());

class App extends React.Component {
    render() {
        return (
            <div>
                <DatePicker hintText="Check-in" minDate={minDate} />
            </div>
        )
    }
}
Share:
10,064
Sathya
Author by

Sathya

Well-qualified Front-End Developer familiar with a wide range of programming utilities and languages. Knowledgeable of frontend and backend development requirements. Able to handle any part of the process with ease. Collaborative team player with excellent technical abilities offering 9+ years of related experience.

Updated on June 25, 2022

Comments

  • Sathya
    Sathya almost 2 years

    I'm creating date range picker with react material ui. My logic behind this functionality is to select required date and if required date has been selected, disable all past dates from selected dates. How to implement this react material ui?

    Here is my code,

    import React from 'react';
    import {render} from 'react-dom';
    import DatePicker from 'material-ui/DatePicker';
    
    function disablePrevDates(date) {
      return date.getDay() === 0;
    }
    
    class App extends React.Component {
        render() {
            return (
                <div>
                    <DatePicker hintText="Check-in" shouldDisableDate={disablePrevDates} />
                </div>
            )
        }
    }
    
    export default App;
    
    • Elon Zito
      Elon Zito over 7 years
      i'd recommend using something like moment.js...or try passing in minDate when you instantiate DatePicker.
    • Oleg Pro
      Oleg Pro over 7 years
      @Sathya could you check my answer, does it solve your question? If it's not what you're asking about could you give more details what you need?