In this tutorial, you will learn about JavaScript date set methods are used to set the date and time according to you. In our previous, we have seen all about JavaScript date object with the help of the example. Here we will cover all the JavaScript date set methods.
Headings of Contents
JavaScript Date Set Methods
JavaScript set methods are used to set the date and time ( years, months, days, hours, minutes, seconds, milliseconds) for a date object.
There are lot of JavaScript set methods available which are provide a facility to set the value in date object.
All the JavaScript date set methods available below.
Method | Description |
---|---|
setFullYear() | Set the year as four-digit |
setMonth() | Set the month as a number ( 0-11 ) |
setDate() | Set the day as a number ( 1-31 ) |
setDay() | Set the weekday as a number ( 0-6 ) |
setSeconds() | Set the seconds as number ( 0-59 ) |
setMilliseconds() | Set the milliseconds as number ( 0-999 ) |
setMinutes() | Set the minutes as number ( 0-59 ) |
setHoures() | Set the houres as number ( 0-23) |
The setFullYear() Method
The setFullYear() method is used to set the year for date object.
Example:
var d = new Date();
d.setFullYear(2020);
console.log(d);
The setFullYear() method can take optionally month and day.
Example:
var d = new Date();
d.setFullYear(2020, 11, 5)
console.log(d);
The setMonth() Method
The setMonth() is used to set the month for date object.
Example:
var d = new Date();
d.setMonth(10)
console(d);
The setDate() Method
The setDate() is used to set the day for the date object as number (1-31).
Example:
var date = new Date();
date.setDate(13);
console.log(d);
The setDay() Method
The setDay() is used to set weekday for the date object as number (0-6).
Example:
var date = new Date();
date.setDay(5);
console.log(date);
The setSeconds() Method
The setSeconds() is used to set the seconds for the date object.
Example:
var d = new Date();
d.setSeconds(50);
console.log(d);
The setMinutes() Method
The setMinutes() is return the minutes for the date object.
Example:
var d = new Date();
d.setMinutes(30);
console.log(d);
The setHoures() Method
The setHoures() method is used to set the hours for the date object.
Example:
var d = new Date();
d.setHoures(10);
console.log(d);
The setMilliseconds() Method
The setMilliseconds() is used to set the milliseconds for the date object.
Example:
var d = new Date();
d.setMilliseconds(120);
console.log(d);
Note:- JavaScript counts months from 0 to 11. January is 0. December is 11.
Conclusion
So, In this article, we have demonstrated all the JavaScript date set methods to set the date and time.
These all methods are very helpful When you are going to set custom date and time in your JavaScript project or code.
In the previous tutorial, we have seen all about JavaScript date get methods to get the information from the JavaScript date object. If this will have the help you, please share it.