question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use locale and dateformat when loading initial values

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

5reactions
mattpagecommented, Nov 28, 2017

+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:

<DatePicker
    selected={moment("12-25-1995", "MM-DD-YYYY")}
    onChange={this.handleChange}
    dateFormat="LLL"
/>

The initial date is not displayed in LLL format.

0reactions
radreamercommented, Jun 15, 2022

Same here for redux-form. I have an initial date "2022-06-30T00:00:00.000Z" which looks the same in the input field: image although I want to apply the default format dateFormat="MM/dd/yyyy" and highlight selected date to have smth like this: image

const [value, setValue] = useState(
  input.value ? new Date(input.value) : null
);

<Datepicker
  name="startDate"
  selected={value}
  disabledKeyboardNavigation
  onChange={date => {
    if (date) {
      date.setHours((-1 * date.getTimezoneOffset()) / 60);
    }
    setValue(date);
  }}
/>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found