JavaScript

JavaScript Date Get Methods

In JavaScript, several methods exist that assist you in retrieving the current system date and time. A “Date” object can return values in milliseconds, seconds, minutes, hours, days, months, and years. You can use the JavaScript Date Get methods for getting the current date and time from the JavaScript and then display it in an HTML document. Moreover, a digital clock can be created by utilizing the Date get methods of JavaScript.

This write-up will discuss the JavaScript Date get methods and their usage in the JavaScript code. Moreover, the examples related to each Date get method such as getTime(), getFullYear(), getMonth(), getDay(), getDate(), getHourse(), getMinutes(), and getSeconds() will be provided. So, let’s start!

JavaScript Date getTime() method

The Date getTime() method in JavaScript outputs a numeric value related to the specified date in universal time. This method will return the number of milliseconds since Epoch or January 1, 1970.

Example: Using JavaScript Date getTime() method

To use the JavaScript getTime() method, firstly create a “Date” object and then call the getTime() method:

const date1 = new Date();

date1.getTime();

The output of the above-given code will show you the number of milliseconds from January 1, 1970, to the current date and time:

JavaScript Date getFullYear() method

The Date getFullYear() output the four-digit value (yyyy) representing the current year of a date in local time.

Example: Using JavaScript Date getFullYear() method

For instance, to display the current year as output, we will execute the following code:

const date1 = new Date();

date1.getFullYear();

Here, we have created a new Date “date1” object, and in the next line, we have called “getFullYear()” method, which will return the current year value:

JavaScript Date getMonth() method

Another built-in method for the Date Object in JavaScript is “getMonth()” method. The “date.getMonth()” returns a number between 0-11, where 0 represents “January” and 11 represents “December”.

Example: Using JavaScript Date getMonth() method

To know more about the working of the Date getMonth() object, write out the following code in your console window:

const date1 = new Date();

date1.getMonth();

From the below-given image, you can see that the output is showing “0,” which indicates that the current month is “January”:

JavaScript Date getDay() method

According to the local time, the “getDay()” Date object method displays the day of the week, and it returns a value between 1 to 7, where 1 represents “Monday,” and 7 represents “Sunday”:

Example: Using JavaScript Date getDay() method

If you want to get the numerical value representing the current day in any of your programs, then you can utilize the below-given code:

const date1 = new Date();

date1.getDay();

The execution of the added code will return “5,” which declares that today is “Friday”:

JavaScript Date getDate() method

getDate()” built-in JavaScript method is used to get the date in numerical format. This Date object method will return a value between 1 to 31.

Example: Using JavaScript Date getDate() method

You can utilize the following code if you are required to return the number of the month’s day:

const date1 = new Date();

date1.getDate();

JavaScript Date getHours() method

The Date object getHours() method is used to get the day’s hours for the current or specified date object value. It returns a value between 0 to 23.

Example: Using JavaScript Date getHours() method

To use JavaScript getHours() method, firstly create a “Date” object, and then call the “getHours()” function in this way:

const date1 = new Date();

date1.getHours();

JavaScript Date getMinutes() method

Similar to the getHours() method, you can also check out the minutes of the current hour in numerical format. The JavaScript Date getMinutes() method permits you to do so and returns a number between 0-59.

Example: Using JavaScript Date getMinutes() method

The below-given code will print out the minutes comprising the current system time:

const date1 = new Date();

date1.getMinutes();

JavaScript Date getSeconds() method

When we have discussed getting the hours and minutes values from the created date object, you must wonder if there is any built-in JavaScript function that will display the seconds of the current minute as a number. The “getSeconds()” method is the answer to your question, and it helps you know about the seconds of the current minute as numbers having values between 0-59.

Example: Using JavaScript Date getSeconds() method

To explore the functionality of JavaScript Date getSeconds() method, you can create a Date object and then utilize it to call out the “getSeconds()” method:

const date1 = new Date();

date1.getSeconds();

Conclusion

The methods to get the date in Javascript are getTime(), getFullYear(), getMonth(), getDay(), getDate(), getHours(), getMinutes(), and getSeconds() for getting current time, year, month, day, date, hours, minutes, and seconds respectively. This write-up discussed the JavaScript Date get methods and their usage in the JavaScript code. Moreover, the examples related to each Date get method such as getTime(), getFullYear(), getMonth(), getDay(), getDate(), getHourse(), getMinutes(), and getSeconds() are also provided in this article.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.