Skip to content
Related Articles

Related Articles

HTML | DOM Time Object
  • Last Updated : 12 Apr, 2019
GeeksforGeeks - Summer Carnival Banner

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:






<!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.




<!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

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :