Javascript - Date setSeconds() Method

The setSeconds() method sets the seconds of a date object.

Description

The setSeconds() method sets the seconds of a date object.

This method can also be used to set the milliseconds.

Syntax

Date.setSeconds(sec,millisec)

Parameter Values

Parameter Require Description
sec Required. An integer representing the seconds
millisec Optional. An integer representing the milliseconds

For second value, the expected values are 0-59, but other values are allowed:

  • -1 will result in the last second of the previous minute
  • 60 will result in the first second of the next minute

Expected values are 0-999, but other values are allowed:

  • -1 will result in the last millisecond of the previous second
  • 1000 will result in the first millisecond of the next second

Return

A Number, representing the number of milliseconds between the date object and midnight January 1 1970

Demo

//Set the seconds to 35:
//display the date-time after changing the seconds.
var d = new Date();
d.setSeconds(35);//from w  w  w  .j av a 2 s.c  om
console.log(d);

//Set both the seconds and milliseconds:
//display seconds and milliseconds after changing both of them.
d.setSeconds(35, 825);
var s = d.getSeconds();
var ms = d.getMilliseconds();
console.log( s + ":" + ms);

Result