It should be possible to set input format
I want to be able to give the date in format DDMMYY (by input) and then display it as DD/MM YYYY or anything else. At the moment the input format must be the same as display format.
@Frezzaldo So you want the user to enter "30022017" and the datepicker to format it as "30/02 2017"? You could try to achieve that behavior by using the onChangeRaw property.
import moment from 'moment';
// Your custom transformation/validation
myCustomFunction = (rawValue) => {
const parsedDate = moment(rawValue, "DDMMYY")
if(parsedDate.isValid()) {
this.setState({selectedDate: parsedDate});
}
}
render() {
<DatePicker
dateFormat="DD/MM YYYY"
selected={this.state.selectedDate}
onChange={this.handleChange}
onChangeRaw={(event) =>
this.myCustomFunction(event.target.value)}
/>
}
Thanks that worked, but now there is another problem. My config looks like this:
<DatePicker
selected={selectedMoment}
onChange={date => this.handlerFunction(date)}
onChangeRaw={event => {this.rawHandlerFunction(event.target.value)}}
className="datepicker"
placeholderText="MMDDYY..."
dateFormat={"D/M YYYY"}
locale={"nb-NO"}
disabledKeyboardNavigation={true}
minDate={momentFromOtherDatepicker}
scrollableYearDropdown={true}
showYearDropdown={true}/>
When clicking on the input the calendar is displayed. Todays date is marked with a light blue color. Then i write "260517" and a dark blue color is marking that date and I also see that the code get "260517". But if I press enter the light blue date (todays date )is then sent as input to the onChange-method and the date is changed back to todays date or whatever was selected before. If I just tab to next input field instead of pressing enter it works as expected.
@Frezzaldo I've created a pr to solve your issue, but it may cause side effects that I'm not aware of. So let's see, what the maintainers are saying.
Fixed by #857