Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Get Date Methods

JavaScript Get Date Methods

In this article, we will know how to get the various date method from the date object in Javascript. There are various methods that retrieve the date in JavaScript. The data values can get like years, months, days, hours, minutes, seconds, milliseconds from a Date Object.

The following is the list of the date method to retrieve the various dates from the Date object in Javascript.

Methods:

  • 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 used to return the number of milliseconds since 1 January 1970.
  • getDay(): It is used to get the weekday as a number (0-6).
  • date.now(): It is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

We will implement the different date methods & understand their usage through the examples.

The getHours() Method: The getHours() method in JavaScript is used to return the hours of a date as a number (0-23).

Example: This example describes the getHours() method for retrieving the hour for the specified date to local time from the date object.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title> JavaScript getHours() Method </title>
</head>
 
<body style="text-align:center;">
    <h1>neveropen</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>


Output:

getHours() Method

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

Example: This example describes the getDate() method for retrieving the current date of the month from the date object.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript getDate() Method</title>
</head>
 
<body style="text-align:center;">
    <h1>neveropen</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>


Output:

getDate() Method

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

Example: This example describes the getMonth() method for retrieving the month from the date object.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript getMonth() Method</title>
</head>
 
<body style="text-align:center;">
    <h1>neveropen</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()+1;
    </script>
</body>
 
</html>


In Javascript, the month number starts from 0 which denotes 1st month ie., January & ends with the month number 11 which denotes the last month ie., December. So, we need to add 1 to get the current month.

Output:

getMonth() Method

The getFullYear() Method: The getFullYear() method in JavaScript is used to return the year (in 4-digit) for the specified date according to local time.

Example: This example describes the getFullYear() method for retrieving the year from the date object.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript getFullYear() Method</title>
</head>
 
<body style="text-align:center;">
    <h1>neveropen</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>


Output:

getFullYear() Method

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

Example: This example describes the getTime() method for retrieving the number of milliseconds from the date object.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript getTime() Method</title>
</head>
 
<body style="text-align:center;">
    <h1>neveropen</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>


Output:

getTime() Method

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

Example: This example describes the getSeconds() method for retrieving the seconds from the date object.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript getSeconds() Method</title>
</head>
 
<body style="text-align:center;">
    <h1>neveropen</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>


Output:

getSeconds() Method

Supported Browsers:

  • Google Chrome 1.0
  • Firefox 1.0
  • Microsoft Edge 12.0
  • Internet Explorer 4.0
  • Opera 3.0
  • Safari 1.0
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments