react js material ui datepicker code example
Example 1: date picker in material ui
const [effectiveSelectedDate, setEffSelectedDate] = useState();
const handleEffDateChange = (date,name) =>{
setEffSelectedDate(date);
}
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
fullWidth
variant='inline'
placeholder='yyyy-MM-dd'
format='yyyy-MM-dd'
margin='normal'
id='date-picker-inline'
name='effectiveDate'
value={effectiveSelectedDate}
onChange={handleEffDateChange}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
/>
</MuiPickersUtilsProvider>
Example 2: material ui datepicke
import React, { Fragment, useState } from "react";
import { TextField } from "@material-ui/core";
import { MobileDatePicker, DesktopDatePicker, DatePicker } from "@material-ui/pickers";
function DatePickersVariants(props) {
const [selectedDate, handleDateChange] = useState(new Date());
return (
<Fragment>
<MobileDatePicker
clearable
label="For mobile"
inputFormat="MM/dd/yyyy"
toolbarPlaceholder="Enter Date"
value={selectedDate}
onChange={date => handleDateChange(date)}
renderInput={props => <TextField {...props} />}
/>
<DesktopDatePicker
autoOk
label="For desktop"
minDate={new Date("2017-01-01")}
value={selectedDate}
onChange={date => handleDateChange(date)}
renderInput={props => <TextField {...props} />}
/>
<DatePicker
disableFuture
label="Responsive"
openTo="year"
views={["year", "month", "date"]}
value={selectedDate}
onChange={date => handleDateChange(date)}
renderInput={props => <TextField {...props} />}
/>
</Fragment>
);
}
export default DatePickersVariants;