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.
In this example, the getFullyear() method is used to retrieve the absolute value of the year.
const year = User_date.getFullYear();
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).
The below code lines utilize the getMonth() method to get the month from the date.
const month = User_date.getMonth();
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:
Where the Date represents the date or a variable (containing date) on which the getDate() method will be applied.
The code makes utilize of the getDate() method to get the date:
const date= User_date.getDate();
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:
const day= User_date.getDay();
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.
The output returns the 6 that represents the Saturday of the week.
Method 5: 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).
Let’s exercise the usage of the getHours() method in the following lines:
let Hours= User_date.getHours();
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.
The following code refers to the use of the getMinutes() method.
let Minutes= User_date.getMinutes();
The output returns a value of 4, which indicates that the current time is in the 4th minute of the hour.