Javascript Date Set Methods

There are different methods which let us set date values for a date object.


setMonth() method

This method is used to set month into a date object which is created using the Date() constructor. It returns the new i.e updated month which is set by setMonth() method.

Input:- 
 <script>  
    let todayDate = new Date();  
    document.writeln("Today's Date is ", todayDate);   
    todayDate.setMonth(11);  
    document.writeln("<br/> New Date is ", todayDate);
</script>

Output:-


setDate() method

This method is used to set date of a month into a date object which are created using date() constructor. This method return the number of milliseconds between the date object and midnight January 1, 1970.

Input:-
 <script>  
    let todayDate = new Date();  
    document.writeln("Today's Date is ", todayDate);   
    todayDate.setDate(15);  
    document.writeln("<br/> New Date is ", todayDate);  
</script>

Output:-


setHours() method

This method is used to set hours into a date object which is created using the Date() constructor. It returns the new date with updated hour which is set by setHours() method.

Input:-
 <script>  
    let todayDate = new Date();  
    document.writeln("Today's Date is ", todayDate);   
    todayDate.setHours(11);  
    document.writeln("<br/> New Date is ", todayDate);
 </script>

Output:-


setMinutes() method

This method is used to set minutes into a Date object which is created using Date() constructor. It returns the new date with updated minute which is set by setMinutes() method.

Input:-
 <script>  
    let todayDate = new Date();  
    document.writeln("Today's Date is ", todayDate);   
    todayDate.setMinutes(52);  
    document.writeln("<br/> New Date is ", todayDate);
</script>

Output:-


setSeconds() method

This method is used to set seconds into a Date object which is created using Date() constructor. It returns the new Date with updated second which is set by setSeconds() method.

Input:-
 <script>  
    let todayDate = new Date();  
    document.writeln("Today's Date is ", todayDate);   
    todayDate.setSeconds(40);  
    document.writeln("<br/> New Date is ", todayDate);
</script>

Output:-


setFullYear() method

This method is used to set year into a date object which is created using Date() constructor. It returns the new date with updated year which is set by setFullYear() method.

Input:-
 <script>  
    let todayDate = new Date();  
    document.writeln("Today's Date is ", todayDate);   
    todayDate.setFullYear(1992);  
    document.writeln("<br/> New Date is ", todayDate);
</script>

Output:-