JavaScript setSeconds Function

JavaScript setSeconds function is used to set the Seconds and Milliseconds of a given date as per local time. The syntax of the JavaScript setSeconds function is:

 Date.setSeconds(Seconds, Milliseconds)

JavaScript setSeconds Function Example

In this setSeconds Function Milliseconds argument is optional. Either you can use it or avoid it. Here, we are using setSeconds to set the seconds to 22 of a current date.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Seconds Functions </title>
</head>
<body>
    <h1> JavaScript setSeconds Function Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt + "<br/>");

  dt.setSeconds(22);
  document.write("After setSeconds() : " + dt);
</script>
</body>
</html>
JavaScript setSeconds Function Example

Date and Time: Thu Nov 08 2018 11:59:48 GMT+0530 (Indian Standard Time)
After setSeconds(): Thu Nov 08 2018 11:59:22 GMT+0530 (Indian Standard Time)

JavaScript set Seconds Function Example 2

In this JavaScript setSeconds example, we set the Seconds of a custom date to 55.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Seconds Functions </title>
</head>
<body>
    <h1> JavaScript setSeconds Function Example </h1>
<script>
  var dt = Date("January 1, 2017 10:11:22");
  document.write("Date and Time : " + dt + "<br/>");

  dt.setSeconds(55);
  document.write("After setSeconds() : " + dt);
</script>
</body>
</html>
JavaScript setSeconds Function 2