Display Full Date in React Using Words in DatePicker - reactjs

I have a very simple problem. I wanted to display the full date words, something like Nov 22th 2020.
Right now, i can only display the Month and the Date
Pls check my codesandbox
CLICK HERE
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<DatePicker
error={false}
helperText={null}
autoOk
fullWidth
inputVariant="outlined"
value={selectedDate}
onChange={handleDateChange}
variant="outlined"
/>
</MuiPickersUtilsProvider>

All you need is add prop format to picker, according to your example, you need format="MMM do yyyy". Here you can find more options

Related

Why does the mui datepicker onChange method revert the format of the date back to the default format

I am using material ui's date picker.
https://mui.com/components/date-picker/#basic-usage
The default format of the datepicker is MM-dd-yyyy (02-23-2022). I want to change the default format to be dd-MM-yyyy` eg. (23-02-2022).
To have it display in the correct format i have used the inputFormat. However i noticed that the onChange value is still an object and therefore i have used the format function from the adapter date-fns to format the value before storing it in state.
However, although the console logs the correct foramt, the value being set in state for the date is still using the old format (MM-dd-yyyy)
What am i doing wrong here. Please see the ccodesandbox for continence https://codesandbox.io/s/basicdatepicker-material-demo-forked-460hxz?file=/demo.js:418-820
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DatePicker
label="Basic example"
value={value}
onChange={(newValue) => {
setValue(format(newValue, "dd-MM-yyyy"));
}}
renderInput={(params) => <TextField {...params} />}
mask="__-__-____"
inputFormat="dd-MM-yyyy"
/>
</LocalizationProvider>
You need to add a 'rifmFormatter' prop, and write a function to handle it. I am working on MUI X, but it is the same as in previous versions. see: https://mui.com/x/api/date-pickers/date-picker/
You don't need to apply any format for the data. inputFormat="dd-MM-yyyy" will take care of everything on MUI Datepicker. This code is working perfectly. Whatever value you are getting is basically a Date type. You can apply format on that.
<DatePicker
views={["day"]}
label="Just date"
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
inputFormat="dd-MM-yyyy"
renderInput={(params) => <TextField {...params} helperText={null} />}
/>;
Attaching the sandbox for reference.

How to show custom text (placeholder) in KeyboardDatePicker

I want to show "-select date-" placeHolder in datepicker if no date defined. Is that possible?
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
disablePast
/>
</MuiPickersUtilsProvider>
You can provide a placeholder in the form of the placeholder prop.
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
placeholder='-select date-'
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
disablePast
/>
</MuiPickersUtilsProvider>
Check out the example on the official documentation page for more info.
OK. the solution is to add labelFunc prop
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
labelFunc={() => selectedDate || '- Select date -'}
disablePast
/>
</MuiPickersUtilsProvider>
or even better (assuming default date value == null):
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
emptyLabel="- Select Date -"
disablePast
/>
</MuiPickersUtilsProvider>
Yes, you can. don't get overwhelmed with null value and stuffs, this is one of many appropriate option you can do.
Try this out:
//You need all of these imports (a must)
import DateFnsUtils from '#date-io/date-fns';
import Moment from 'moment';
import { MuiPickersUtilsProvider, KeyboardDatePicker } from "#material-ui/pickers";
//you need this too (a must)
const [selectDate, handleChangeSelectDate] = useState(null);
//Finally this is final thing you need
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDatePicker
id="date-picker-inline"
autoOk
placeholder='-select date-'
inputStyle={{ textAlign: 'center' }}
variant="inline"
inputVariant="outlined"
format="MMM/dd/yyyy" //Can use MM if you want to show only number of month
value={selectDate? moment(selectDate) : null}
InputAdornmentProps={{ position: "start" }} //just use to put calendar icon on start or left position - remove it if you want it on end of the box or right hand side
onChange={date => handleChangeSelectDate(date)}
/>
</MuiPickersUtilsProvider>

No Calender in react mui Datepicker/Calender

Im trying to use the mui's onscreen always on Calender component and it says that there is no component here in the pickers folder like that, but I found some answers saying that material ui has an inbuild onscreen Calender ex: here
I found the answer - refer to this document .
Using the code like this variant="static";
<KeyboardDatePicker
disableToolbar
variant="static"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
label="Date picker inline"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
"aria-label": "change date",
}}
/>

KeyboardDateTimePicker Material UI Not null validation

I have implemented KeyboardDateTimePicker. I am not able to validate it for empty or null values? I tried few things for validation, yet its not working.
Code:
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDateTimePicker
required={true}
format="dd/MM/yyyy HH:mm"
value={this.props.StartDate}
placeholder="dd/mm/yyyy hh:mm"
onChange={(event) => this.props.handleDateTime(event)}
/>
</MuiPickersUtilsProvider>
I tried placing required, validated=true in form control, yet its not working.
Did not find any keywords here :
https://material-ui-pickers.dev/api/KeyboardDatePicker
About DateTimePicker:
https://material-ui-pickers.dev/demo/datetime-picker
If anyone has faced similar issue please suggest.
Add props error and helperText
You can make a function checkErrors=()=>this.props.StartDate?false:true and use props
error={checkErrors()}
helperText={checkErrors():"Some error message":""}
Your code may looking something like this:
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<KeyboardDateTimePicker
required={true}
format="dd/MM/yyyy HH:mm"
value={this.props.StartDate
}
placeholder="dd/mm/yyyy hh:mm"
onChange={(event) => this.props.handleDateTime(event)}
error={this.props.StartDate?false:true}
helperText={this.props.StartDate?"Some error message":""}
/>
</MuiPickersUtilsProvider>

Pickers Material ui, error displayed ONLY in english language

Into my react application I'm using material UI pickers and I want that the errors will be displayed in Italian language. The calendar it's ok with the Italian and I specify it in this way thanks to moment:
import * as moment from 'moment'
import 'moment/locale/it'
but this declaration seems not working for pickers. Can someone help me?
updated with working example =>
https://60qb9.csb.app/
You can use the invalidDateMessage prop of the KeyboardDatePicker:
<KeyboardDatePicker
disableToolbar
variant="inline"
format="MM/dd/yyyy"
margin="normal"
id="date-picker-inline"
label="Date picker inline"
value={selectedDate}
onChange={handleDateChange}
KeyboardButtonProps={{
"aria-label": "change date"
}}
invalidDateMessage="Computer says no"
/>
Live example here: https://codesandbox.io/s/material-picker-set-invalid-date-message-rn2in

Resources