Home > Web Front-end > JS Tutorial > body text

JavaScript method setTime() to set the Date object in milliseconds

黄舟
Release: 2017-11-07 14:09:55
Original
2632 people have browsed it

Definition and Usage

The setTime() method sets the Date object in milliseconds.

Syntax

dateObject.setTime(millisec)
Copy after login
ParametersDescription
millisec Required. The date and time to be set are the number of milliseconds between midnight GMT January 1, 1970. A millisecond value of this type can be passed to the Date() constructor , which can be obtained by calling the Date.UTC() and Date.parse() methods. Representing the date in milliseconds makes it time zone independent.

Return value

Return parameter millisec. Before ECMAScript was standardized, this method did not return a value.

Tips and Notes:

Note: This method is always used in conjunction with a Date object.

Example

Example 1

In this example we will add 77771564221 milliseconds to 1970/01/01 and display the new date and time:

<script type="text/javascript">

var d = new Date()
d.setTime(77771564221)
document.write(d)

</script>
Copy after login

Output:

Mon Jun 19 1972 11:12:44 GMT+0800 (中国标准时间)
Copy after login

Example 2

In this example we will subtract 77771564221 milliseconds from 1970/01/01 and display the new date and time:

<script type="text/javascript">

var d = new Date()
d.setTime(-77771564221)
document.write(d)

</script>
Copy after login

Output:

Sun Jul 16 1967 04:47:15 GMT+0800 (中国标准时间)
Copy after login

The javascript Date.setTime() method sets the time represented by the Date object in milliseconds since January 1, 1970 00:00:00 UTC.

Here are the details of the parameters:

  timeValue : 表示自1970年1月00:00:00 UTC起的一个整数(毫秒数)。
Copy after login

Example:

<html>
<head>
<title>JavaScript setTime Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date( "Aug 28, 2008 23:30:00" );
  dt.setTime( 5000000 );
  document.write( dt ); 
</script>
</body>
</html>
Copy after login

This will produce the following results:

Thu Jan 1 06:53:20 UTC+0530 1970
Copy after login

The above is the detailed content of JavaScript method setTime() to set the Date object in milliseconds. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!