HTML | DOM Time Object

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Before Clicking On Button:

After Clicking On Button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Code_Mech, shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.