Search code examples
javascriptreactjsmomentjsiso8601reactstrap

Convert hh:mm to ISO 8601 with current date in Javascript / Momen.js


I have a variable filterStartHour which depends on a user input (Reactstrap Input of type time) I'm trying to convert the input which is in the hh:mm format (e.g 14:32) to ISO 8601 with the current date (e.g 2020-06-29T14:32:00.000Z).

This is what I have tried.

this.setState({ filterStartHour: moment(this.state.filterStartHour).format() }) // Or with .toISOString() 

But they return either Invalid date or null respectively.


Solution

  • Or you just tell moment the format of the input:

    const time = '15:42';
    const isoDate = moment.utc(time, "HH:mm");
    
    console.log(isoDate.toISOString());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>