I'm trying to send dates range to backend node server. Everything works just fine and the time I'm choosing in my calendar is correctly picked, ie. dates I'm choosing in the calendar are the same just before sending it to the backend. But when I'm checking my payload inside console's network the day is not correct anymore - it is always one day earlier. For some reason I don't know dates are changed with that one day.
Here's my chunk of code:
import React, { Component } from 'react';
import Calendar from 'react-calendar';
import { connect } from 'react-redux';
import * as actions from '../../actions';
class Holidays extends Component {
state = { date: [new Date(), new Date()], dates: [] }
calculateRange(dateRange) {
const getDates = (startDate, endDate) => {
let dates = [],
currentDate = startDate,
addDays = function(days) {
const date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
const dates = getDates(new Date(dateRange[0]), new Date(dateRange[1]));
this.setState({ dates });
}
onChange = date => this.calculateRange(date[0], date[1]);
handleClick() {
const { dates } = this.state;
console.log(dates); // -----here still I'm getting correct values
const { sendDateRange } = this.props;
sendDateRange({ dates });
}
render() {
return (
<div className="centered-column">
<h1>Holidays</h1>
<Calendar
onChange={(date) => this.calculateRange(date)}
value={this.state.date}
locale="pl"
returnValue="range"
selectRange={true}
/>
<a className="btn margin__vertical" onClick={() => this.handleClick()}>BOOK</a>
</div>
);
}
}
export default connect(null, actions)(Holidays);
For example here's my log (at the position the same as above) when trying to choose 20th and 21st of August.
[Mon Aug 20 2018 00:00:00 GMT+0200 (Central European Summer Time), Tue Aug 21 2018 00:00:00 GMT+0200 (Central European Summer Time)]
By my payload shows this:
"2018-08-19T22:00:00.000Z", "2018-08-20T22:00:00.000Z"