Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clarity on how to coerce the date from moment to an ISO date string #610

Closed
evolve2k opened this issue Oct 27, 2016 · 3 comments
Closed

Comments

@evolve2k
Copy link

evolve2k commented Oct 27, 2016

I had issues setting up react-datepicker as the component deals with moment objects but this does not play so well when used with the rest of my app that is using Redux and is also sending request to an external API.

I think this is a common setup requirement to have the date available to the rest of the react app in a string format once the date has been picked.

With some help from a friend we got this working but it definitley took a bit of mucking around having moment objects available in certain places and date strings available in other places.

Here is the code I got working for anyone having these issues, would be great to include a few of these clues in the README or tips also just not quite sure what to emphasise or where this should best be put.

Open to suggestions and happy to create a pull request to update documentation if that would be welcome.

import React, { PropTypes } from 'react'
import moment from 'moment'
import DatePicker from 'react-datepicker'

export default class DateField extends React.Component {
  static propTypes = {
    value: PropTypes.string,
    onChange: PropTypes.func,
    onBlur: PropTypes.func
  };

  constructor (props) {
    super(props)
    this.state = {
      value: props.value
    }
    this.handleChange = this.handleChange.bind(this)
  }

  handleChange (date) {
    const formattedDate = date && date.format()
    this.setState({value: formattedDate})
    this.props.onChange(formattedDate)
  }

  render () {
    const {value} = this.state
    const attrs = {}
    if (value) {
      attrs.selected = moment(value)
    }
    return (
      <DatePicker
        type="datetime"
        dateFormat="DD MMM YYYY"
        onBlur={this.props.onBlur}
        onChange={this.handleChange}
        {...attrs}
        />
    )
  }
}





@aij
Copy link
Contributor

aij commented Mar 17, 2017

date.format() will actually give you an ISO timestamp. `date.format('YYYY-MM-DD') will give you an ISO 8601 date.

@nbkhope
Copy link

nbkhope commented Aug 15, 2017

I had the same issues you had. My redux application state should store a string while the onChange for the date picker has a Moment object as argument and the value prop for the picker takes a moment object, not a string. Thank you for for code sample.

@martijnrusschen
Copy link
Member

Moment.js was removed in #1527, starting v2.0.0, this package doesn't use Moment anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants