JavaScript setTime() 方法


定义和用法

setTime() 方法以毫秒设置 Date 对象。

语法
  1. dateObject.setTime(millisec)
参数 描述
millisec 必需。要设置的日期和时间据 GMT 时间 1970 年 1 月 1 日午夜之间的毫秒数。这种类型的毫秒值可以传递给 Date() 构造函数,可以通过调用 Date.UTC() 和 Date.parse() 方法获得该值。以毫秒形式表示日期可以使它独立于时区。
返回值

返回参数 millisec。在 ECMAScript 标准化之前,该方法不返回值。


提示和注释:

注释:该方法总是结合一个 Date 对象来使用。


实例

例子 1

在本例中,我们将向 1970/01/01 添加 1000000000 毫秒,并显示新的日期和时间:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var d = new Date()
  5. d.setTime(1000000000)
  6. document.write(d)
  7. </script>
  8. </body>
  9. </html>
例子 2

在本例中,我们将从 1970/01/01 减去 1000000000 毫秒,并显示新的日期和时间:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var d = new Date()
  5. d.setTime(-1000000000)
  6. document.write(d)
  7. </script>
  8. </body>
  9. </html>

分类导航