score:1

Accepted answer

with the below code block, you are setting the state and immediately you are trying to access the updated value, but state updates done in async fashion. you will get the latest value in the next re-render.

    ...
    setstarttime(moment(timethestart).format("yyyy-mm-dd"))
    setendtime(moment(timetheend).format("yyyy-mm-dd"))

    // these 2 logs prints initial value, not updated value.
    console.log(starttime);
    console.log(endtime)
   ...

you can use a useeffect to log or do something with latest values of starttime and endtime.

useeffect(() => {
  console.log("starttime", starttime);
  console.log("endtime", endtime);
}, [starttime, endtime]);


Related Query

More Query from same tag