Home >Web Front-end >JS Tutorial >JavaScript method setTime() to set the Date object in milliseconds

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

黄舟
黄舟Original
2017-11-07 14:09:552700browse

Definition and Usage

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

Syntax

dateObject.setTime(millisec)
Parameters Description
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>

Output:

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

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>

Output:

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

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起的一个整数(毫秒数)。

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>

This will produce the following results:

Thu Jan 1 06:53:20 UTC+0530 1970

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!

Statement:
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