Home >Web Front-end >JS Tutorial >JavaScript method setSeconds() to set a specified time in seconds
Definition and usage
The setseconds() method is used to set a specified number of seconds.
Syntax
dateObject.setSeconds(sec,millisec)
Parameters | Description |
sec | Required. A value representing seconds as an integer between 0 and 59. |
millisec | Optional. A value representing milliseconds, between 0 and 999. Before EMCAScript was standardized, this parameter was not supported. |
Return value
Adjusted date expressed in milliseconds. Before ECMAScript was standardized, this method returned nothing.
Tips and Notes:
Note: If one of the above parameters is specified with a single digit, JavaScript will add one or two leading 0s to the result.
Note: This method is always used in conjunction with a Date object.
Example
In this example, we will set the seconds field of the current time to 01 through the setSeconds() method:
Output:
Tue Nov 07 2017 13:18:01 GMT+0800 (中国标准时间)
If the msValue parameter is not specified, the value returned from the method using getMilliseconds is used. If the specified argument is outside the expected range, calling setSeconds attempts to update the Date object's latest information accordingly. For example, if you use secondsValue with a value of 100, the minutes stored in the date object will be incremented by 1, and 40 will be used for seconds.
Example:
<html> <head> <title>JavaScript setSeconds Method</title> </head> <body> <script type="text/javascript"> var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setSeconds( 80 ); document.write( dt ); </script> </body> </html>
This will produce the following results:
Thu Aug 28 23:31:20 UTC+0530 2008
The above is the detailed content of JavaScript method setSeconds() to set a specified time in seconds. For more information, please follow other related articles on the PHP Chinese website!