Below is the example of Date setSeconds() method.
// Here a date has been assigned
// while creating Date object
'October 13, 1996 05:35:32'
// New second of 52 is being set in above Date
// Object with the help of setSeconds() method
// New second from above Date Object is
// being extracted using getSeconds()
B = dateobj.getSeconds();
// Printing new Second
The date.setSeconds() method is used to set seconds into a Date object which is created using Date() constructor.
Parameter: This method accepts a single parameters as mentioned above and described below:
- seconds_Value: This parameter holds the value of second which is used to set in Date() constructor.
Return Value: It returns the new Date with updated second which is set by setSeconds() method.
Note: The DateObj is a valid Date object created using Date() constructor in which we want to set the second. Value of second is from 0 to 59.
More codes for the above method are as follows:
Program 1: If in Date() constructor we do not give any second, still setSeconds() method set new second which is given as its parameter.
Example 2: If nothing as parameter is given in Date() constructor, still setSeconds() method set second but month, year, date etc become current ones. Here 42 is the new second, 3 is the current month i.e April, 1 is the current date and 2018 is the current year.
42 3 1 2018
Example 3: If value of second 66 is given as the parameter of setSeconds() function, It will set 6 as the second because second range is from 0 to 59 and ( 66%60 = 6).
Here 6 is the new second and minutes become 36 from 35 because the second range is from 0 to 59. So when 66 is passed as a parameter to the setSeconds() function, it will set the seconds in the respective Date object to (66%60 = 6) and will increment the minutes in the respective Date object by (66/60 = 1).
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 3 and above
- Opera 3 and above
- Safari 1 and above