Using Javascript to Enter Time in an HTML5 Time Field

I am trying to create a function in JS that populates html5 <input type=time> format similar to this hh:mm:ss .

 function timeChange(){ var d = new Date(); d.getHours(); d.getMinutes(); d.getSeconds(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time_in").value = (....); } 

I am not sure how to encode .value for this. I tried using .value = (hours":"minutes":"seconds); but it just gives me a compilation error.

Has anyone got any ideas? I just need this in hh: mm: ss.

HTML5 Code:

  <button type="button" onClick="timeChange()">Time</button> <input id="time_in" type="time" name="time_in"> 
+4
source share
4 answers

hours":"minutes":"seconds not a string concatenation, you need + s: hours+":"+minutes+":"+seconds

+5
source
 var d = new Date(); // Need to create UTC time of which fields are same as local time. d.setUTCHours(d.getHours(), d.getMinutes(), d.getSeconds(), 0); document.getElementById("time_in").valueAsDate = d; 
+1
source
 document.getElementById('time_in').value = hours + ":" + minutes + ":" + seconds; 

Otherwise, you are not creating a single concatenated string.

0
source

The easiest way is to grab a time string from a Date object:

var time = (new Date()).toTimeString().split(' ')[0];

Separation allows us to remove part of the time region of a row.

0
source

Source: https://habr.com/ru/post/1447035/


All Articles