JavaScript | Get Date Methods

There are various methods to get the date in JavaScript. The data values can get like years, months, days, hours, minutes, seconds, milliseconds from a Date Object.

Method:

  • getDate(): It is used to get the day as a number (1-31).
  • getFullYear(): It is used to get the year.
  • getHours(): It is used to get the hour (0-23).
  • getMilliseconds(): It is used to get the milliseconds (0-999).
  • getMinutes(): It is used to get the minutes (0-59).
  • getMonth(): It is used to get the month (0-11).
  • getSeconds(): It is used to get the seconds (0-59).
  • getTime(): It is used to get the time.
  • getDay(): It is used to get the weekday as a number (0-6).
  • Date.now(): It is used to get the time.
  • The getHours() Method: The getHours() method in JavaScript is used to return the hours of a date as a number (0-23).

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head
        <title>
            JavaScript getHours() Method
        </title>
    </head
      
    <body style="text-align:center;">
      
        <h1>GeeksForGeeks</h1>
          
        <h2>JavaScript getHours()</h2>
      
        <p id="GFG"></p>
          
        <!-- Script to use getHours() method -->
        <script>
            var d = new Date();
              
            document.getElementById("GFG").innerHTML
                    = d.getHours();
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

  • The getDate() Method: The getDate() method in JavaScript is used to return the day of a date as a number (1-31).

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head
        <title>
            JavaScript getDate() Method
        </title>
    </head
      
    <body style="text-align:center;">
      
        <h1>GeeksForGeeks</h1>
          
        <h2>JavaScript getDate()</h2>
      
        <p id="GFG"></p>
          
        <!-- Script to use getDate() method -->
        <script>
            var d = new Date();
              
            document.getElementById("GFG").innerHTML
                    = d.getDate();
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

  • The getMonth() Method: The getMonth() method in JavaScript is used to return the month of a date as a number (0-11).

    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head
        <title>
            JavaScript getMonth() Method
        </title>
    </head
      
    <body style="text-align:center;">
      
        <h1>GeeksForGeeks</h1>
          
        <h2>JavaScript getMonth()</h2>
      
        <p id="GFG"></p>
          
        <!-- Script to use getMonth() method -->
        <script>
            var d = new Date();
              
            document.getElementById("GFG").innerHTML
                    = d.getMonth();
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

  • The getFullYear() Method: The getFullYear() method in JavaScript is used to return the number of milliseconds.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head
        <title>
            JavaScript getFullYear() Method
        </title>
    </head
      
    <body style="text-align:center;">
      
        <h1>GeeksForGeeks</h1>
          
        <h2>JavaScript getFullYear()</h2>
      
        <p id="GFG"></p>
          
        <!-- Script to use getFullYear() method -->
        <script>
            var d = new Date();
              
            document.getElementById("GFG").innerHTML
                    = d.getFullYear();
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

  • The getTime() Method: The getTime() method in JavaScript is used to return the number of milliseconds.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head
        <title>
            JavaScript getTime() Method
        </title>
    </head
      
    <body style="text-align:center;">
      
        <h1>GeeksForGeeks</h1>
          
        <h2>JavaScript getTime()</h2>
      
        <p id="GFG"></p>
          
        <!-- Script to use getTime() method -->
        <script>
            var d = new Date();
              
            document.getElementById("GFG").innerHTML
                    = d.getTime();
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

  • The getSeconds() Method: The getSeconds() method in JavaScript returns the seconds of a date object (0-59).

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head
        <title>
            JavaScript getSeconds() Method
        </title>
    </head
      
    <body style="text-align:center;">
      
        <h1>GeeksForGeeks</h1>
          
        <h2>JavaScript getSeconds()</h2>
      
        <p id="GFG"></p>
          
        <!-- Script to use getSeconds() method -->
        <script>
            var d = new Date();
              
            document.getElementById("GFG").innerHTML
                    = d.getSeconds();
        </script>
    </body>
      
    </html>                    

    chevron_right

    
    

    Output:

full-stack-img




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.