My React app recieves date in the following JSON-format:
[
{"date":"20201001","time":"001100"},
{"date":"20201001","time":"001200"},
{"date":"20201001","time":"001300"}
]
I want to save date and time to React state in normal format - date plus time. So it should be only date like this:
[
{"date":"2020-10-01 00:11:00"},
{"date":"2020-10-01 00:12:00"},
{"date":"2020-10-01 00:13:00"}
]
Here is my App.js code:
import React from 'react';
import { render } from 'react-dom';
import { timeParse } from "d3-time-format";
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {datas: []};
}
componentDidMount() {
this.getAllData();
}
getAllData() {
fetch("http://localhost:8080/data")
.then(response => response.json())
.then((data) => { data = tt(data)})
.then((data) => {
this.setState({datas: data});
});
};
render() {
if (this.state == null) {
return <div>Loading data...</div>
}
return (
<div>{this.state.datas ? this.state.datas.toString() : 'No data yet...'}</div>
)
}
}
function tt(d) {
d.date = timeParse(d.date+d.time);
return d;
};
const parseDateTime = timeParse("%Y%d%m%H%M%S");
render(
<MyComponent />,
document.getElementById("root")
);
export default MyComponent;
I believe something wrong in the following line of code:
.then((data) => { data = tt(data)})
but cant understand what.
I always get unmodified date and time, same format it is recieved from server.