Home > Software engineering >  Convert the input (type="time") to Time instead of String
Convert the input (type="time") to Time instead of String

Time:01-13

I am getting time input in the format of string. I need to convert it to datetime type in order to send it to the API. How can I convert a string to datetime?

<input
 placeholder="enter time"
 type="time"
 onChange={(e) => setTime(e.target.value)}
/>

Need to convert Time to datetime datatype.

CodePudding user response:

change your input type to :

type="datetime"

CodePudding user response:

You can add your time in a new Date type like :

<input
 placeholder="enter time"
 type="time"
 onChange={setTime(this)}
/>

function setTime(time) {
 const today = new Date();
 console.log(new Date(today.toDateString()   ' '   time.value));
}

JSFiddle

CodePudding user response:

there is a great documentation about datetimes MDN Web docs. Otherwise have a look at this related post.

Best xderes

  •  Tags:  
  • Related