When creating applications it is often required to display the date and time of a region where the application is being used in. JavaScript allows its user to play with dates by using date object. A JavaScript date object is a representation of time and there is a huge range of JavaScript methods that are associated with the date object. These methods are broadly classified into two categories which are as follows.
Let’s explore the methods that fall under the above-mentioned categories.
JavaScript Get Date Object Methods
The JavaScript methods that are used to fetch values such as year, month, day, etc, are referred to as the Get Date Object Methods. Here we have discussed these methods in-depth.
getDate() Method
For the purpose of fetching the day of the date in numbers, the getDate() method is used.
Syntax
Example
The example below demonstrates the working of getDate() method. We have first created a new date object by the name “date” and then used a variable “fetch” to extract the date.
The date has been fetched.
getFullYear() Method
In order to extract the full year of a date, the getFullYear() method is used.
Syntax
Example
We are first creating a date object and then simply using the getFullYear() method, we are printing the full year of the date object.
The full year of the date object is 2022.
getMonth() Method
This method is used to fetch the month of the date in the form of numbers.
Syntax
Example
In the code below, we are using the getMonth() method to fetch the month of the date object.
The month has been fetched successfully.
getDay() Method
To fetch the weekday of the date in numeric form, the getDay() method is used.
Syntax
Example
The example below demonstrates the working of the getDay() method.
The number 4 represents the 4th day of the week.
getHours() Method
As the name suggests, the getHours() method is used to extract the hour of the date object.
Syntax
Example
Here is how you can use the getHours() method.
The hours have been fetched in numeric form.
getMinutes() Method
For the purpose of fetching the minutes, the getMinutes() method is used.
Syntax
Example
By creating a date object first you can simply use the getMinutes() method to fetch the minutes of the date object.
The minutes were fetched successfully.
getSeconds() Method
In order to extract the seconds, the getSeconds() method is used.
Syntax
Example
The example below demonstrates the working of the getSeconds() method.
The getSeconds() method is working properly.
getMilliseconds() Method
This method is used to extract the milliseconds of a date.
Syntax
Example
Here is how you use the getMilliseconds() method.
The milliseconds were fetched successfully.
getTime() Method
This method is used to extract the time in the date object in milliseconds.
Syntax
Example
This example illustrates how to use the getTime() method.
The time has been fetched in milliseconds.
JavaScript Set Date Object Methods
The JavaScript methods that are used to set values such as year, month, das, etc, are referred to as the Set Date Object Methods. Below we have explained these methods in detail.
setDate() Method
For the purpose of setting the day of a date object, the setDate() method is used.
Syntax
The day is a required parameter which is an integer from 1-31.
Example
The example below demonstrates how you can create a new date object and then use the setDate() method to modify the initial date.
The new date has been set.
setFullYear() Method
In order to set the full year of a date object, the setFullYear() method is used. It can also set the month and day of the date object.
Syntax
The year is a required parameter, however, the month and day are optional parameters.
Example
This example demonstrates the working of the setFullYear() method. We have created a new date object first and then extracted the year of that date object. We then used the setFullYear() method to set the new year of the object to 2023.
The new year has been set.
setMonth() Method
For the purpose of setting the month of a date object the setMonth() method is used.
Syntax
The month is a required parameter accepting integer values from 0-11, however, the day is an optional parameter accepting values from 1-31.
Example
In the example mentioned below, a new date object has been created and the month of that object is being fetched. Now using the setMonth() method we are setting the new of the object.
The new month has been set.
setHours() Method
As the name suggests the setHours() method is used to set the hours of a date object.
Syntax
The hour is a required parameter accepting integer values from 0-23. However, the min, and sec are optional parameters accepting values from 0-59. The millisec is also an optional parameter that renders values from 0-999.
Example
In the code below, after creating a new date object we are extracting the hours of that object and then using the setHours() method we are setting the new hours to 23.
The new hours have been set.
setMinutes() Method
The method that is used to set the minutes of a date object is referred to as the setMinutes() method.
Syntax
The min is a required parameter that exhibits values from 0-59. The sec also accepts values from 0-59 but it is an optional parameter. Meanwhile, the millisec is also an optional parameter that renders values from 0-999.
Example
We are setting a new date object and fetching its minutes as well. Afterward, we are using the setMinutes() method to set new minutes to 20.
The new minutes have been set.
setSeconds() Method
The method that is used to set the seconds of a date object is referred to as the setSeconds() method.
Syntax
The sec is a required parameter that exhibits values from 0-59. Meanwhile, the millisec is an optional parameter that renders values from 0-999.
Example
To demonstrate the working of the setSeconds() method, we are first creating a new date object and extracting the seconds of that date object. Then we are using the setSeconds() method to set new seconds for the same date object.
The new seconds have been set.
setMilliseconds() Method
The method that is used to set the milliseconds of a date object is referred to as the setMilliseconds() method.
Syntax
The millisec is a required parameter that renders values from 0-999.
Example
In the example below we are creating a date object by the name “date” then we are fetching the milliseconds of that date object. Afterward, using the setMilliseconds() method we are setting the new milliseconds to 97.
The milliseconds have been set.
setTime() Method
For the purpose of setting the time in the date object in milliseconds started from epoch(1 January 1970), the setTime() method is used.
Syntax
The milliseconds is a required parameter.
Example
This example illustrates how to use the getTime() method.
The time has been set in milliseconds.
Conclusion
JavaScript date object methods are broadly classified into two categories which are get date object methods and set date object methods. As the name suggests, these methods can be used to either fetch or set the date and time of a date object method. Some methods that lie under the group of get date object methods are getDate(), getFullYear(), getHour(), etc. Meanwhile, some methods that are classified into the category of set object methods are setDate(), setFullYear(), setHour() etc. Methods that fall under both of these categories are explained in-depth in this tutorial.