Javascript DOM HTML Time dateTime Property get

Introduction

Get the date that a <time> element represents:

var x = document.getElementById("myTime").dateTime;

Click the button to return the value of the datetime attribute of the time element.

The time element is supported by any of the major browsers.

The dateTime property is only supported in Firefox and Opera 12.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>This is by birthday: <time id="myTime" datetime="2020-02-14">Valentines day</time>.</p>

<button onclick="myFunction()">Test</button>
<p id="demo"></p>

<script>
function myFunction() {/*www  . j a  v  a  2s  .  com*/
  var x = document.getElementById("myTime").dateTime;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

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

The datetime attribute gets the date or time being specified.

This attribute is used if no date or time is specified in the element's content.

The datetime attribute is supported by any of the major browsers.

Value
Description
YYYY-MM-DDThh:mm:ssTZD The date or time being specified.
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)
TZD - Time Zone Designator (Z denotes Zulu, which is Greenwich Mean Time)

The dateTime property return a String representing a machine-readable form of the element's date and time.




PreviousNext

Related