How to get the day of the month in JavaScript?


In this tutorial, we will learn how to find out the day of the month using JavaScript.

We'll go over the most popular methods which are used for obtaining the name of the month and day in JavaScript. It is important that starting to install packages for something basic may be frustrating if you are working on a tiny project.

JavaScript's built-in methods should be used to implement small applications; dependencies should only be added when absolutely necessary

Using the getDate() Method

A built-in data type for working with dates and times is the Date object. The new Date () constructor creates the Date object, which comes with a number of built-in methods for managing and presenting that data. A new Date instance, by default, produces an object that is the current Date and time (i.e., as determined by the computer's system settings). We apply the getDate() method on the date object to find the day only.

Syntax

Following is the syntax for the getDate() method −

new Date().getDate(); // current date
new Date("October 31, 2021 09:38:00").getDate() // custom date

We have created the date object in the above syntax and accessing the date using getDate() property.

Example

In this example, we can see how using the getDate() method, we can print the current Date as per device timing. Here, we have created two date objects.

For the day1, we are taking the computer's real-time value, and for the day2, we find the day being provided by the user in the program. This way, we get the day of the month using built-in methods.

Here is an example which formats a date as MM-DD-YYYY hh:mm:ss using the getDate* method.

<html> <body> <h3>Get the day of month using <i>getDate()</i> method</h3> <p id = "output1"></p> <p id = "output2"></p> </body> <script> // Get the current date let day1 = new Date().getDate(); document.getElementById("output1").innerHTML = day1; //current date // Get a day in the week of a certain date let day2 = new Date("October 31, 2021 09:38:00").getDate(); document.getElementById("output2").innerHTML = day2; //31 </script> </html>

Using the getUTCDate() Method

The time established by the World Time Standard is known as ,UTC (Universal Time Coordinated). GMT and UTC are the same time zones (Greenwich Mean Time).

All JavaScript getUTC() methods presumptively use local time for the Date. This function returns the date object's day of the month, ranging from 1 to 31. The day according to UTC, is returned by the getUTCDate() function.

Syntax

Following is the syntax for the getUTCDate() method −

new Date().getUTCDate(); // current date
new Date("October 31, 2021 09:38:00").getUTCDate () // custom date

In the above syntax, we have used the getUTCDate() property of the date object.

Example

In this example, we'll show how to print the current Date in accordance with device timing by utilizing the getUTCDate() method.

We utilize the computer's real-time value for the day1 and the day that the user entered into the software for day2, respectively.

Hence we try to print the current Date in accordance with device timing by utilizing the getUTCDate() method. By using built-in methods, we may obtain the day of the month in this fashion.

Here is an example which formats a date as MM-DD-YYYY hh:mm:ss using the getUTC* method. By using built-in methods, we may obtain the day of the month in this fashion.

<html> <body> <h3>Get the day of month using <i>getDate()</i> method</h3> <p id = "output1"></p> <p id = "output2"></p> </body> <script> // Get the current date let day1 = new Date().getUTCDate(); document.getElementById("output1").innerHTML = day1; //current date // Get a day in the week of a certain date let day2 = new Date("September 29, 2021 09:38:00").getUTCDate(); document.getElementById("output2").innerHTML = day2; //31 </script> </html>

In this tutorial, we have learned that to find the day of the month using JavaScript's inbuilt methods getDate() and getUTCDate().

The getDate() method gives the Date to the user's screen in the Greenwich Mean Time (GMT) zone.

The getUTCDate() shows the Date according to the Universal Time Coordinated (UTC) zone. Both these methods show the same time for any region and device timing in the whole world.

Updated on: 26-Aug-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements