Reputation:
I have a date/time in this format "07/04/2018 12:24 PM". I want to convert it to format to string like "July 24, 2018 12:24 PM" before submit it to server using moment.js.
saveEditMobile = async()=> {
await this.setDateTimebeforeSubmit().then(axios.put(host+"audience/"+this.state.CurrentEditMobile,this.state))
}
setDateTimebeforeSubmit = () => {
return new Promise(resolve => {
const prev_process_date = moment(this.state.process_date, 'MM/DD/YYYY h:mm A').format('MMMM DD, YYYY h:mm A')
this.setState({
process_date: prev_process_date,
})
resolve()
})}
However, after I ran the above code. The data I sent to server was 2018-07-10T17:55:00.000Z. I don't know want happen to my code. Process_date value in the current state was July 24, 2018 12:24 PM. How do I resolve this?
Upvotes: 2
Views: 3140
Reputation: 287
moment('2016-03-12 13:00:00').format('LLL')
output :"March 13, 2016 2:00 PM"
for further information you can use given link : https://momentjs.com/guides/
Upvotes: 0
Reputation: 112777
setState
is asynchronous, so you need to make sure the state has updated if you plan on using this.state
directly after. You could pass resolve
as the second argument to setState
to resolve the promise when the state has updated.
You also want to give a function to then
, not invoke the axios request straight away.
You can also use the LLL
moment format to get the desired output.
saveEditMobile = async () => {
await this.setDateTimebeforeSubmit().then(() => {
axios.put(host + "audience/" + this.state.CurrentEditMobile, this.state);
});
};
setDateTimebeforeSubmit = () => {
return new Promise(resolve => {
const prev_process_date = moment(
this.state.process_date,
"MM/DD/YYYY h:mm A"
).format("LLL");
this.setState({ process_date: prev_process_date }, resolve);
});
};
Upvotes: 1