6

Hoe to set up a value, to input type = time , set in html using value attribute value.

I have tried:

var tm =  new Date();

var in= "<input type='time' name='time' id='tm' value='" + tm + "'/>";

it does not work

3
  • javascript, I'm assuming? What makes you think you can simply stuff a JS OBJECT into a string that happens to contain an html input definition? Commented May 23, 2013 at 16:10
  • what is a proper way to do it? That an easy way to convert an object to string, will automatically call toString() for that particular object. Commented May 23, 2013 at 16:14
  • yes, but how is the JS engine to know how to format that stringified date? yyyy-mm-dd? dd-mm-yy? unix timestamp? read up on the JS Date object ... Commented May 23, 2013 at 16:15

1 Answer 1

6

Use the valueAsDate attribute to convert between a Date object and an input element’s value.

> var elem = document.createElement('input');
> elem.type = 'time';
> elem.valueAsDate = new Date(1970, 1, 1, 7, 34);
> elem.value
"12:34"
Sign up to request clarification or add additional context in comments.

2 Comments

Anyway to pass a string like 11:30am?
Yes you can pass in any valid RFC 3339 time string to elem.value - w3.org/TR/html-markup/input.time.html

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.