Home > Web Front-end > JS Tutorial > JavaScript method setSeconds() to set a specified time in seconds

JavaScript method setSeconds() to set a specified time in seconds

黄舟
Release: 2017-11-07 13:20:01
Original
3115 people have browsed it

Definition and usage

The setseconds() method is used to set a specified number of seconds.

Syntax

dateObject.setSeconds(sec,millisec)
Copy after login
ParametersDescription
sec Required. A value representing seconds as an integer between 0 and 59.
millisecOptional. 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 (中国标准时间)
Copy after login

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>
Copy after login

This will produce the following results:

Thu Aug 28 23:31:20 UTC+0530 2008
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template