The setSeconds
function in JavaSript is a method of the Date
class. The function sets the second for the date that is specified in accordance with local time.
Date.setSeconds(seconds, milliseconds);
The function takes in two parameters.
In the above snippet of code, seconds
and milliseconds
are integers.
milliseconds
is an optional parameter
The expected values for seconds
and milliseconds
are 0 to 59 and 0 to 999 respectively. If we pass a value greater than 59 in place of seconds
, it updates the minute accordingly.
If a value greater than 999 is passed in milliseconds
, the value of seconds is updated.
// an object of Date type is created to apply the function onvar obj = new Date('January 1, 2001 09:24:21');// Time is printed in H:M:S format before the seconds are setconsole.log("TIME BEFORE FUNCTION CALL: " + obj.getHours() + " : " + obj.getMinutes() + " : " + obj.getSeconds())// setSeconds function is called with an unexpected value. It'll update the values like the following comments// new_seconds = (64 % 60)// new_minutes = old_minutes + (64 / 60)obj.setSeconds(64);// Time is printed in H:M:S format after the function callconsole.log("TIME BEFORE FUNCTION CALL: " + obj.getHours() + " : " + obj.getMinutes() + " : " + obj.getSeconds())
In the above example, the time is printed in H:M:S format. An unexpected value is passed to the setSeconds
function. Its effect can be observed on the value of minutes after the function call.
// an object of Date type is created to apply the function onvar obj = new Date('January 1, 2001 09:24:21');// Time is printed in H:M:S format before the seconds are setconsole.log("TIME BEFORE FUNCTION CALL: " + obj.getHours() + " : " + obj.getMinutes() + " : " + obj.getSeconds()+ " : " + obj.getMilliseconds())// setSeconds function is called with an unexpected value.obj.setSeconds(45, 32);// Time is printed in H:M:S format after the function callconsole.log("TIME BEFORE FUNCTION CALL: " + obj.getHours() + " : " + obj.getMinutes() + " : " + obj.getSeconds()+ " : " + obj.getMilliseconds())
In the above example, the time is printed in H:M:S:MS format. The value of seconds
and milliseconds
is set using the setSeconds
function. The effect of the function can be observed on seconds and milliseconds after the function is called.
Free Resources