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
Function bound to onChange receives wrong date #226
Comments
Sounds like this is likely an issue with confusion between UTC and local time. function handler(newDate) {
var dateStr = newDate.toString();
} If so, you can achieve the result you want by first casting to UTC like so: function handler(newDate) {
var dateStr = newDate.utc().toString();
} |
Thanks! Sounds reasonable. I'll try that. |
Probably my code that errors, but the first time I select a date, the handler gives me the day before. If I select again, a correct date is given. |
Can you post a snippet of your code that's interacting with the datepicker? |
I stripped the code from unrelated stuff. With this code I still get the weird functionality.
|
If I remove shouldComponentUpdate(), I get "t.format is not a function" |
I suspect this is because you're storing handleFinishedReadingChange(date) {
console.log("ii: " + date.utc().toString());
this.setState({
finishedReading: date.utc(),
});
} |
Even if I remove the call to setState(), I still get the same. |
If I remove "onChange={this.handleFinishedReadingChange}" then the date is as expected. |
If you remove the Your Can you try something like this: import React, {Component} from 'react';
import DatePicker from 'react-datepicker';
import moment from 'moment';
export default class BookForm extends Component {
constructor(props) {
super(props);
this.state = {
// Initial value is the current time in UTC
// Additionally, subsequent moments should all be in UTC now
finishedReading: moment.utc()
}
}
handleFinishedReadingChange(date) {
console.log("ii: " + date.toString());
this.setState({
finishedReading: date,
});
}
shouldComponentUpdate(nextProps, nextState) {
// Test dates instead of id
// Or, chain a series of needed conditionals
return this.state.date != nextState.date;
}
render() {
return (
<div className="component">
<form className="form-horizontal">
<div className="form-group">
<label htmlFor="finishedReading" className="col-sm-2">Finished reading</label>
<div className={'col-sm-8'}>
<DatePicker
selected={this.state.finishedReading}
dateFormat="DD.MM.YYYY"
onChange={::this.handleFinishedReadingChange}
/>
</div>
</div>
</form>
</div>
);
}
} |
+1 @ryanwilsonperkin for helping each other out |
Apparently setting a proper utc value in constructor "fixed" this. However, I don't want to use a default date. |
@janimattiellonen what happens if you initially pass in |
Did you guys resolve this issue? |
Sorry, no. I switched to jQuery datepicker :( |
I'm sorry that we couldn't help you. |
No problems. Thanks for your time. |
The problem happens when you pass anything that is not a moment object to DatePicker.selected prop. |
@pablolmiranda, I'm not sure what there is to fix; passing anything that is neither a moment object nor |
Good point. This is why the 'Clear datepicker input' example is broken. |
@pablolmiranda taking this discussion to #627 |
I'm also running into the same issue as @janimattiellonen: when I click on a day for the first time, it brings me to the day before. However, when I click on it again, it selects the correct day. Similar to his situation, I want to have an initial state for the date to be Is there a workaround for this? |
Will duplicate also here from #627 (comment) Guys, I have the same issue with wrong date.
Has anyone found a fix for this issue with wrong date? I'm using react-datepicker v.0.48.0 |
so ummm anyone figure this out @vladimir-konnov? |
This is definitely late but how I ended up getting the correct date was to do something like
which I found on another issue that I cannot find anymore. I'll link it when I do find it. |
When I select 2015-11-01 the function bound to onChange event gives me "2015-10-31T22:00:00.000Z".
Code used:
I'm using 0.15.2.
The text was updated successfully, but these errors were encountered: