Javascript Reference - HTML DOM Time dateTime Property








The datetime attribute sets the date or time if no date or time is specified in the element's content.

The dateTime property sets or gets the datetime attribute in a <time> element.

Browser Support

dateTime No No Yes No Yes

Syntax

Return the dateTime property.

var v = timeObject.dateTime 

Set the dateTime property.

timeObject.dateTime=YYYY-MM-DDThh:mm:ssTZD




Property Values

Value Description
YYYY-MM-DDThh:mm:ssTZD The date or time being specified.
  • YYYY - year (e.g. 2014)
  • MM - month (e.g. 01 for January)
  • DD - day of the month (e.g. 02)
  • T - a required separator for time part
  • hh - hour (e.g. 22 for 10.00pm)
  • mm - minutes (e.g. 51)
  • ss - seconds (e.g. 03)
  • TZD - Time Zone Designator

Return Value

A String type value representing a machine-readable form of the element's date and time.

Example

The following code shows how to get the date that a <time> element represents.


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

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the date and time of a <time> element.


<!DOCTYPE html>
<html>
<body>
<time id="myTime" datetime="2014-02-28T08:00Z">my day</time>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from   ww w  .j  a  v a  2  s .  co m-->
    document.getElementById("myTime").dateTime = "2014-06-24T09:30Z";
}
</script>

</body>
</html>

The code above is rendered as follows: