5

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">

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Envious No worries, it happens to all of us
1
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;

Comments

0

The simplest way would be to grab the time string from the Date object:

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

The split allows us to remove the timezone portion of the string.

Comments

0
document.getElementById('time_in').value = hours + ":" + minutes + ":" + seconds;

Otherwise you're not creating one concatenated string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.