Is there any way to implement a month Picker and year picker using Material UI. In month view the output should be like only month and the year only Eg:- 2020-09
Month and Year picker in Material UI React
45.1k Views Asked by Sindujan Nirmalan At
        	3
        	
        There are 3 best solutions below
0
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                In your component, pass an array called views with month and year.
views={["year", "month"]}
Take a look at views prop for more info: https://material-ui-pickers.dev/api/KeyboardDatePicker
Also change the format to MM/yyyy
format="MM/yyyy"
Here is a sandbox for your reference:
1
                 On
                        
                            
                        
                        
                            On
                            
                                                    
                    
                import "date-fns";
import React from "react";
import Grid from "@material-ui/core/Grid";
import DateFnsUtils from "@date-io/date-fns";
import { MuiPickersUtilsProvider, DatePicker } from "@material-ui/pickers";
export default function MaterialUIPickers() {
  // The first commit of Material-UI
  const [selectedDate, setSelectedDate] = React.useState<Date | null>(
    new Date("2014-08-18T21:11:54")
  );
  const handleDateChange = (date: Date | null) => {
    setSelectedDate(date);
  };
  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <Grid container justify="space-around">
<DatePicker
  variant="inline"
  openTo="year"
  views={["year", "month"]}
  label="Year and Month"
  helperText="Start from year selection"
  value={selectedDate}
  onChange={handleDateChange}
/>
      </Grid>
    </MuiPickersUtilsProvider>
  );
}
Second Solution
import "date-fns";
import React from "react";
import Grid from "@material-ui/core/Grid";
import DateFnsUtils from "@date-io/date-fns";
import {
  MuiPickersUtilsProvider,
  KeyboardDatePicker
} from "@material-ui/pickers";
export default function MaterialUIPickers() {
  const [selectedDate, setSelectedDate] = React.useState(
    new Date("2014-08-18T21:11:54")
  );
  const handleDateChange = (date) => {
    setSelectedDate(date);
  };
  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <Grid container justify="space-around">
        <KeyboardDatePicker
          disableToolbar
          variant="inline"
          format="MM/yyyy"
          views={["year", "month"]}
          margin="normal"
          id="date-picker-inline"
          label="Date picker inline"
          value={selectedDate}
          onChange={handleDateChange}
          KeyboardButtonProps={{
            "aria-label": "change date"
          }}
        />
      </Grid>
    </MuiPickersUtilsProvider>
  );
}
Material UI V5
Material UI v5 added the
DatePickerto@mui/labso you don't need to install the third-party package anymore. To restrict to month and year only, you can set theviewprop like this:To change how the
TextFielddisplay the current date, useinputFormatprop. If you're using date-fns, see this table here for reference.Material UI V4
You can use different views as demonstrated in this section here.
Live Demo