Use locale and dateformat when loading initial values
See original GitHub issueWhen loading react-datepicker with redux form, sometimes there will be some initial values provided by the user, when we load these into the datepicker I believe (please correct me if I am wrong) that we actually ignore any requested display formats or locales provided by the user if they supply the selected
prop.
Offending lines of code appears to be here: https://github.com/Hacker0x01/react-datepicker/blob/master/src/index.jsx#L180-L183
As I say, this is only when the datepicker is first initialised, locale and dateFormat is recognised onChange 😃
/**
*
* Datepicker
*
*/
import 'react-datepicker/dist/react-datepicker.css'
import React from 'react'
import PropTypes from 'prop-types'
import ReactDatepicker from 'react-datepicker'
import moment from 'moment'
moment.locale('en-gb')
class Datepicker extends React.Component {
state = {
selectedDate: moment(this.props.input.value),
}
handleChange = (date) => {
this.setState({
selectedDate: date,
})
this.props.input.onChange(date)
}
render() {
const { input, meta, placeholder } = this.props
const filterDates = (date) => moment(date) >= moment().startOf('day')
return (
<ReactDatepicker
{...input}
className="form-control"
filterDate={filterDates}
placeholderText={placeholder}
isClearable
locale="en-gb"
showMonthDropdown
showYearDropdown
dropdownMode="select"
dateFormat='DD/MM/YYYY'
selected={this.state.selectedDate}
onChange={this.handleChange}
onBlur={input.onBlur}
/>
)
}
}
Datepicker.propTypes = {
input: PropTypes.object,
meta: PropTypes.object,
placeholder: PropTypes.string,
}
export default Datepicker
My current code incase anyone would like to point out I’m doing something wrong 😃
Thanks guys, doing an awesome job!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:5
Top Results From Across the Web
java - SimpleDateFormat and locale based format string
The LocalDate class represents a date-only value without time-of-day and without time zone. ... The java.time classes use ISO 8601 formats by default...
Read more >Set a locale or region for data (Power Query) - Microsoft Support
For more information see Create, load, or edit a query in Excel. Right click a column header, and then select Change Type >...
Read more >Get data format and calendar by locale - Angular - Syncfusion
I am trying to get ejDatePicker calendar and display date by locale. I set ej-datepicker property locale to different locales like 'fr-FR', 'lt ......
Read more >Java SimpleDateFormat - Java Date Format - DigitalOcean
A DateFormat object can be created using the getDateInstance() and getTimeInstance() method of the DateFormat class. Locale loc = new Locale("en ...
Read more >DateFormat (Java Platform SE 7 ) - Oracle Help Center
DateFormat provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
+1 this is a pain. The value in selected should receive the dateFormat prop. I am not able to find a workaround for supplying a moment object to selected and have it display initially in the correct format. In my case, I have dateFormat set to ‘lll’.
This does not work for me:
The initial date is not displayed in LLL format.
Same here for redux-form. I have an initial date
although I want to apply the default format 
"2022-06-30T00:00:00.000Z"
which looks the same in the input field:dateFormat="MM/dd/yyyy"
and highlight selected date to have smth like this: