...

Web Technologies - JAVASCRIPT

Back to Course

Lesson Description


Lession - #225 Javascript Date Set Method



In JavaScript, there are several techniques for setting the date. For a Date Object, data values like as years, months, days, hours, minutes, seconds, and milliseconds can be set.

  • setDate(>
    is a method for setting the date as a number (1-31>
    .
  • setFullYear(>
    is a method for setting the year (optionally month and day>
    .
  • setHours(>
    is a function that is used to set the time (0-23>
    .
  • setMilliseconds(>
    is a method for adjusting the milliseconds (0-999>
    .
  • setMinutes(>
    is a method for setting the minutes (0-59>
    .
  • setMonth(>
    is a function that is used to set the month (0-11>
    .
  • setSeconds(>
    is a method for setting the seconds i.e javascript wait for 1 second(0-59>
    .
  • setTime(>
    is a function that is used to set the time.


1>
The setSeconds(>
method is used to set the seconds of a date object (0-59>




 <!DOCTYPE html>
<html>

<head> 
    <title>
        JavaScript setSeconds(>
method </title> </head> <style> body{ background-color:#98ff98;} </style> <body style="text-align:center;"> <br><br> <h2>JavaScript setSeconds(>
</h2> <p id="GFG"></p> <!-- Script to use setSeconds method --> <script> var d = new Date(>
; d.setSeconds(22>
; document.getElementById("GFG">
.innerHTML = d; </script> </body> </html>


Output:
Sorry cannot be displayed
Try it here



2>
The setMinutes(>
method is used to set the minutes of a date object (0-59>
.



<!DOCTYPE html>
<html>
  
<head> 
    <title>
        JavaScript setMinutes(>
method </title> <style> body{ background-color:#e9ffdb;} </style> </head> <body style="text-align:center;"> <br><br> <h2>JavaScript setMinutes(>
</h2> <p id="GFG"></p> <!-- Script to use setMinutes method --> <script> var d = new Date(>
; d.setMinutes(2>
; document.getElementById("GFG">
.innerHTML = d; </script> </body> </html>



Output:
Sorry cannot be displayed
Try it here



3>
The setHours(>
method is used to set the hours of a date object (0-23>



       

<!DOCTYPE html>
<html>
  
<head> 
    <title>
        JavaScript setHours(>
method </title> <style> body{ background-color:#e9ffdb;} </style> </head> <body style="text-align:center;"> <br><br> <h2>JavaScript setHours(>
</h2> <p id="GFG"></p> <!-- Script to use setHours(>
method --> <script> var d = new Date(>
; d.setHours(2>
; document.getElementById("GFG">
.innerHTML = d; </script> </body> </html>



Output:
Sorry cannot be displayed
Try it here



4>
   The setDate(>
Method: In JavaScript, the setDate(>
method is used to set the day of a date object (1-31>

   
 <!DOCTYPE html>
<html>
  
<head> 
    <title>
        JavaScript setDate(>
method </title> <style> body{ background-color:#e9ffdb;} </style> </head> <body style="text-align:center;"> <br><br> <h2>JavaScript setDate(>
</h2> <p id="GFG"></p> <!-- Script to use setDate method --> <script> var d = new Date(>
; d.setDate(5>
; document.getElementById("GFG">
.innerHTML = d; </script> </body> </html>



Output:
Sorry cannot be displayed
Try it here



5>
  The setMonth(>
Method: The setMonth(>
method in JavaScript is used to set the month of a date object (0-11>



  
<!DOCTYPE html>
<html>
  
<head> 
    <title>
        JavaScript setMonth(>
method </title> </head> <style> body{ background-color:#e9ffdb;} </style> <body style="text-align:center;"> <br><br> <h2>JavaScript setMonth(>
</h2> <p id="GFG"></p> <!-- Script to use setMonth method --> <script> var d = new Date(>
; d.setMonth(5>
; document.getElementById("GFG">
.innerHTML = d; </script> </body> </html>


Output:
Sorry cannot be displayed
Try it here



6>
   The setFullYear(>
Method: The setFullYear(>
method in JavaScript is used to set the year of a date object.


<!DOCTYPE html>
<html>
  
<head> 
    <title>
        JavaScript setFullYear(>
method </title> </head> <style> body{ background-color:#e9ffdb;} </style> <body style="text-align:center;"> <br><br> <h2>JavaScript setFullYear(>
</h2> <p id="GFG"></p> <!-- Script to use setFullYear method --> <script> var d = new Date(>
; d.setFullYear(2022>
; document.getElementById("GFG">
.innerHTML = d; </script> </body> </html>


Output:
Sorry cannot be displayed
Try it here