Javascript Reference - HTML DOM Time Object








The Time object represents an HTML <time> element.

Time Object Properties

Property Description
Sets or gets the datetime attribute in a <time> element

Standard Properties and Events

The Time object supports the standard properties and events.

Example

We can access a <time> element by using getElementsByTagName().


<!DOCTYPE html>
<html>
<body>
<time id="myTime" datetime="2014-02-28">my day</time>.</p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from   w w  w.  ja  v a  2s  .  c om-->
    var x = document.getElementById("myTime").dateTime;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

We can create a <time> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<head>
</head><!--from   ww  w . jav  a2  s.  c  om-->
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    var x = document.createElement("TIME");
    x.setAttribute("datetime", "2014-02-28");
    var t = document.createTextNode("My day");
    x.appendChild(t);
    document.body.appendChild(x);
}
</script>

</body>
</html>

The code above is rendered as follows: