Javascript DOM HTML Input DatetimeLocal defaultValue Property

Introduction

Change the default value of a datetime field:

document.getElementById("myLocalDate").defaultValue = "2020-01-02T11:42:42.510";

Click the button to change the default value of the datetime field.

View in separate window

<!DOCTYPE html>
<html>
<body>

Date: <input type="datetime-local" id="myLocalDate" value="2014-11-16T15:25:33">
<button onclick="myFunction()">Test</button>
<script>
function myFunction() {/*from   ww  w . ja va  2  s  .  c  o  m*/
  document.getElementById("myLocalDate").defaultValue = "2020-01-02T11:42:42.510";
}
</script>

</body>
</html>

The defaultValue property sets or gets the default value of a local datetime field.

The default value is the value set in the HTML value attribute.

The defaultValue contains the default value, while value contains the current value.

If there are no changes, defaultValue and value is the same.

The defaultValue property accepts and returns a string type value.

String format:

Value
Description
YYYY-MM-DDThh:mm:ss.ms Explanation of components:
YYYY - year (e.g. 2020)
MM - month (e.g. 01 for January)
DD - day of the month (e.g. 08)
T - a required separator if time is also specified
hh - hour (e.g. 22 for 10.00pm)
mm - minutes (e.g. 55)
ss - seconds (e.g. 03)
ms - milliseconds (e.g. 123)



PreviousNext

Related