JavaScript Get Date Methods

10 Nov, 2021
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>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>
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>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>
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>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()+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>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>
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>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>
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>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>
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
bhaskargeeksforgeeks
javascript-date
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
How to append HTML code to a div using JavaScript ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?