JavaScript

JavaScript Get Date Methods

The JavaScript Get Date methods allow you to retrieve the minute, hour, day, month, or a year from a specific time period. These methods are utilized to perform a micro as well as macro analysis from a specific date/month/year to a specific date/month/year. The application of Get-Date methods can be observed on various well-known websites, including stock exchanges and trading websites.

In this post, we will learn the working and usage of the various Get Date methods.

Method 1: getFullYear()

The getFullYear() method returns the output of the year in an absolute number. The method relies on the local time. Here, we will describe the syntax and an example of the getFullyear() method.

Syntax
 

Date.getFullYear()

 
Example

In this example, the getFullyear() method is used to retrieve the absolute value of the year.

const User_date = new Date('December 25, 1995 23:15:30');
const year = User_date.getFullYear();
console.log(year);

 
In the above code, a predefined value of the data is stored in the variable and then the getFullyear() method is applied to that variable.


The output shows that only the year number is printed on the console.

Method 2: getMonth()

The getMonth() method is used to return an integer value present between 0 and 11. The 0 shows the first month (January), and the 11 represents the last month (December).

Syntax

Date.getMonth()

 
Using the getMonth() method, an example is given of how to utilize the getMonth() method in JavaScript.

Example

The below code lines utilize the getMonth() method to get the month from the date.

const User_date = new Date('July 25');
const month = User_date.getMonth();
console.log(month);

 
In the above code, a variable stores the date and then the getMonth() method is applied to this variable


It is observed that only the month number is returned.

Method 3: getDate()

The getDate() method is utilized to extract the date of the month. It returns the integer values between 1 and 31 that show the first and last dates of the month. The syntax of the getDate() method is as follows:

Syntax

Date.getDate()

 
Where the Date represents the date or a variable (containing date) on which the getDate() method will be applied.

Example

The code makes utilize of the getDate() method to get the date:

const User_date = new Date('July 25');
const date= User_date.getDate();
console.log(date);

 
A random date is stored in a variable, and then the getDate() method is applied to the Date variable.


From the output, it is observed that the date (25) is retrieved and printed on the console.

Method 4: getDay()

The getDay() method extracts the day of the week. It returns a value from 0 to 6. The number 0 is used for Sunday and 6 for Saturday. The syntax of the getDay() method is as follows:

Syntax

Date.getDay()

 
Example

An example is followed by utilizing the getDay() method in JavaScript.

const User_date = new Date('July 25 2020');
const day= User_date.getDay();
console.log(day);

 
The statement in the third line of code stores the day of the week using the day variable. The last statement is written in the code to present the information of the day using the. getDay() method.

Output


The output returns the 6 that represents the Saturday of the week.

Method 5: getHours()

The getHours() method basically gives the hour information. It retrieves the hour value from the date provided. The method extracts values from 0 to 23. The syntax of the getHours() method in JavaScript is as follows:

Syntax

Date.getHours()

 
The get.Hours() method is applied on the Date (it may be a variable containing the date or directly a date can be provided).

Example

Let’s exercise the usage of the getHours() method in the following lines:

let User_date = new Date('July 25 2020, 08:04:20');
let Hours= User_date.getHours();
console.log(Hours);

 
In the above code, the getHours() method is utilized to give information between 0 and 23 in JavaScript.

Output


The display shows the value of 8, which represents the 8th hour.

Method 6: getMinutes()

The getMinutes() method is used to retrieve information for minutes within an hour. It extracts integer values from 0 to 59. The syntax for utilizing this method is below.

Syntax

getMinutes()

 
In JavaScript, extract the minutes information using the getMinutes() method.

Example

The following code refers to the use of the getMinutes() method.

let User_date = new Date('July 25 2020, 08:04:20');
let Minutes= User_date.getMinutes();
console.log(Minutes);

 
Using the getMinutes() method, retrieve the minutes information of an hour in JavaScript.

Output


The output returns a value of 4, which indicates that the current time is in the 4th minute of the hour.

Conclusion

JavaScript supports Get Date methods to retrieve information about the date, time, month, year, etc. The getDate(), getMonth(), getYear(), getHours()and getMinutes() methods are discussed in this post. All these methods aim to return the date/time in a specific format. For better understanding, we have provided the syntax and examples of each method.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.