Date Set Methods in JavaScript

In JavaScript there are some methods to setting a part of a date:.

There is a list of JavaScript set date methods with their description.

Methods Description
setDate() It sets the day value for the specified date on the basis of local time.
setDay() It sets the particular day of the week on the basis of local time.
setFullYears() It sets the year value for the specified date on the basis of local time.
setHours() It sets the hour value for the specified date on the basis of local time.
setMilliseconds() It sets the millisecond value for the specified date on the basis of local time.
setMinutes() It sets the minute value for the specified date on the basis of local time.
setMonth() It sets the month value for the specified date on the basis of local time.
setSeconds() It sets the second value for the specified date on the basis of local time.
setUTCDate() It sets the day value for the specified date on the basis of universal time.
setUTCDay() It sets the particular day of the week on the basis of universal time.
setUTCFullYears() It sets the year value for the specified date on the basis of universal time.
setUTCHours() It sets the hour value for the specified date on the basis of universal time.
setUTCMilliseconds() It sets the millisecond value for the specified date on the basis of universal time.
setUTCMinutes() It sets the minute value for the specified date on the basis of universal time.
setUTCSeconds() It sets the second value for the specified date on the basis of universal time.

Example of setFullYear() Method

In this example setFullYear() method us used to set the year of a date as a four digit number:

<script type = "text/javascript">
var d = new Date();
d.setFullYear(2020)
document.write(d); 
</script>                  
        

Output of above code


Example of setMonth() Method

In this example setMonth() method us used to set the month of a date as a number (0-11):

<script type = "text/javascript">
var d = new Date();
d.setMonth(11);
document.write(d);
</script>                  
        

Output of above code


Example of setDate() Method

In this example setDate() method is used to set the day of a date as a number (1-31):

<script type = "text/javascript">
var d = new Date();
d.setDate(15);
document.write(d);
</script>                  
        

Output of above code


Example of setHours() Method

In this example setHours() method is used to set the hours of a date as a number (0-23):

<script type = "text/javascript">
var d = new Date();
d.setHours(22);
document.write(d);
</script>                  
        

Output of above code


Example of setMinutes() Method

In this example setMinutes() method is used to set the minutes of a date as a number (0-59):

<script type = "text/javascript">
var d = new Date();
d.setMinutes(30);
document.write(d);
</script>                  
        

Output of above code


Example of setSeconds() Method

In this example setSeconds() method is used to set the seconds:

<script type = "text/javascript">
var d = new Date();
d.setSeconds(30);
document.write(d);
</script>                  
        

Output of above code








The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.