Open In App

HTML | DOM Time Object

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The DOM Time Object is used to represent the HTML Time element . The Time element is accessed by getElementById().
Properties: 
 

  • dateTime: It contains single attribute datetime which is used to set or return the date/time in a machine-readable form of the element.

Syntax: 
 

document.getElementById("ID");

Where “id” is the ID assigned to the “time” tag.
Example-1: 
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM Time Object</title>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM Time Object</h2>
     Jawahar lal Nehru birthday is celebrated on
    <!--  Assigning id to time tag.  -->
    <time Id="GFG" datetime="2018--11-14 12:00">
      children's day.
    </time>
    <br>
    <br>
   
    <button onclick="myGeeks()">Submit</button>
    <p id="sudo"></p>
 
   
    <script>
        function myGeeks() {
            var g = document.getElementById("GFG").innerHTML;
            document.getElementById("sudo").innerHTML = g;
        }
    </script>
</body>
 
</html>


Output:
Before Clicking On Button: 
 

After Clicking On Button: 
 

Example-2: <Time> Object can be created by using the document.createElement method. 
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM Time Object</title>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM Time Object</h2>
    <br>
    <button onclick="myGeeks()">Submit</button>
    <script>
        function myGeeks() {
           
            // time tag created
            var g =
             document.createElement("TIME");
           
            g.setAttribute(
            "datetime", "2018--11-14 12:00");
           
            var f =
            document.createTextNode("Children's day");
           
            g.appendChild(f);
           
            document.body.appendChild(g);
 
        }
    </script>
</body>
 
</html>


Output :
Before Clicking On Button: 
 

After Clicking On Button : 
 

Supported Browsers: The browser supported by DOM Time Object are listed below: 
 

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

 



Last Updated : 24 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads