JavaScript Date Methods

The JavaScript methods for Set/Get times are as follows :

  • getDate()
  • setDate()
  • getFullYear()
  • setFullYear()
  • getHours()
  • setHours()
  • getMilliseconds()
  • setMilliseconds()
  • getMinutes()
  • setMinutes()
  • getMonth()
  • setMonth()
  • getSeconds()
  • setSeconds()

getDate() and setDate() :

The getDate() method returns the day of a date as a number (1-31) and setDate() method sets the day of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current Date : ")
    document.write(d.getDate());
    document.write("<br/>New Date :");
    d.setDate(10);
    document.write(d.getDate());
  </script> 
</body>
</html>
Output :



getFullYear() and setFullYear() :

The getFullYear() method returns the year of a date as a four digit number and setFullYear() sets the year of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current Year : ")
    document.write(d.getFullYear());
    document.write("<br/>Current Year : ");
    d.setFullYear(2035);
    document.write(d.getFullYear());
  </script> 
</body>
</html>
Output :



getHours() and setHours() :

The getHours() method returns the hours of a date as a number (0-23) and setHours() sets the hours of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current hour : ")
    document.write(d.getHours());
    document.write("<br/>New hour :");
    d.setHours(23);
    document.write(d.getHours());
  </script> 
</body>
</html>
Output :



getMilliseconds() and setMilliseconds() :

The getMilliseconds() method returns the milliseconds of a date as a number (0-999) and setMilliseconds sets the milliseconds of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current Time in MS : ")
    document.write(d.getMilliseconds());
    document.write("<br/>New Time in MS :");
    d.setMilliseconds(845);
    document.write(d.getMilliseconds());
  </script> 
</body>
</html>
Output :



getMinutes() and setMinutes() :

The getMinutes() method returns the minutes of a date as a number (0-59) and The setMinutes() sets the minutes of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current Time in Minutes : ")
    document.write(d.getMinutes());
    document.write("<br/>New Time in Minutes : ");
    d.setMinutes(59);
    document.write(d.getMinutes());
  </script> 
</body>
</html>
Output :



getMonth() and setMonth() :

The getMonth() method returns the month of a date as a number (0-11) and setMonth() sets the month of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current month : ")
    document.write(d.getMonth());
    document.write("<br/>New Month : ");
    d.setMonth(10);
    document.write(d.getMonth());
  </script> 
</body>
</html>
Output :



getSeconds() and setSeconds() :

The getSeconds() method returns the seconds of a date as a number (0-59) adn setSeconds sets the seconds of a date object.

Example :
<html>
<body>
  <script>
    var d = new Date();
    document.write("Current Time in seconds : ")
    document.write(d.getSeconds());
    document.write("<br/>New Time in seconds : ");
    d.setSeconds(30);
    document.write(d.getSeconds());
  </script> 
</body>
</html>
Output :



Next Topic :