Assign date javascript html5 datetime-local input
DOM:
<input id="myTextbox" type="datetime-local" /> Javascript (jQuery):
$('#myTextbox').val(new Date().toISOString()); Does not work. The input format [type = datetime-local] must be ISO 8601, which returns javascript Date.toISOString ().
I recommend using input.valueAsDate or input.valueAsNumber instead of input.value.
http://www.w3schools.com/jsref/jsref_toisostring.asp :
The toISOString () method converts a Date object to a string using the ISO standard.
The standard is called ISO-8601, and the format is: YYYY-MM-DDThh: mm: ss.sssZ
Although ISO 8601 has some flexibility, the javascript Date toISOString () format is exactly as shown above.
A āZā at the end means that it is a UTC date. Thus, this view includes time zone information. (Javascript dates, of course, are in UTC, as they are represented worldwide in milliseconds from the era.)
HTML5 input format with type = datetime-local should be ...
The following parts are in exactly the following order:
- Date.
- The literal string is "T".
- Time.
Example:
1985-04-12T23: 20: 50.52
1996-12-19T16: 39: 57
http://www.w3.org/TR/html-markup/input.datetime-local.html
This is still ISO 8601, but more strict, and it does not allow you to specify the time zone.
Fortunately, removing the time zone is as easy as removing the final āZā.
var isoStr = new Date().toISOString(); $('#myTextbox').val(isoStr.substring(0,isoStr.length-1));