admin管理员组

文章数量:1355540

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

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

Share Improve this question edited May 23, 2013 at 16:48 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked May 23, 2013 at 16:08 Emmanuel NEmmanuel N 7,4472 gold badges29 silver badges36 bronze badges 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? – Marc B 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. – Emmanuel N 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 ... – Marc B Commented May 23, 2013 at 16:15
Add a ment  | 

1 Answer 1

Reset to default 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"

本文标签: javascriptInput time type time setting valueStack Overflow