Open In App

JavaScript Get Date Methods

JavaScript Date Object allows us to get and set the date values.

In this article, we will know how to get the various date methods from the date object in Javascript. There are various methods that retrieve the date in JavaScript. The date values can get like years, months, days, hours, minutes, seconds, and 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:



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

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.




let d = new Date();
console.log(d.getHours());

Output
4

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.




let d = new Date();
console.log(d.getDate());

Output
1

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.




let d = new Date();
console.log(d.getMonth() + 1);

Output
8

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.

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.




let d = new Date();
console.log(d.getFullYear());

Output
2023

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.




let d = new Date();
console.log(d.getTime());

Output
1690865918886

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.




let d = new Date();
console.log(d.getSeconds());

Output
38

Supported Browsers:


Article Tags :