-1

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.

  • 1
    What do you mean by `datetime` datatype? If you are using any framework, backend, library etc. please also add to your tags. – Ricky Mo Jan 12 '22 at 09:04
  • 1
    What do you mean by "datetime type"? Are you talking about a DateTime object in PHP? Well then _create_ one, from the given string value, in your PHP code. – CBroe Jan 12 '22 at 09:04
  • If I create a Date object, I get an object. Is there any time or datetime datatype in JS? – Husnain Mehmood Jan 12 '22 at 09:06
  • Pass a string to the API and convert it to the appropriate type there. – juliomalves Jan 13 '22 at 08:21

2 Answers2

0

change your input type to :

type="datetime"
AkifObaidi
  • 17
  • 1
  • 9
0

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

Francois Borgies
  • 2,378
  • 31
  • 38