Using Javascript to input time in HTML5 time field

14,964

Solution 1

hours":"minutes":"seconds isn't concatenating the string, you need +s: hours+":"+minutes+":"+seconds

Solution 2

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;
Share:
14,964
Envious
Author by

Envious

Updated on June 05, 2022

Comments

  • Envious
    Envious almost 2 years

    I'm trying to create a function in JS that will fill in the html5 <input type=time> with a format like 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'm not sure how to code the .value for this. I've tried using .value = (hours":"minutes":"seconds); but that just gives me a compile error.

    Anyone got ideas? I just need it in hh:mm:ss.

    HTML5 code:

     <button type="button" onClick="timeChange()">Time</button>
    
     <input id="time_in" type="time" name="time_in">
    
  • freejosh
    freejosh over 11 years
    @Envious No worries, it happens to all of us