aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/date/now/index.html
blob: b7153612a29721d9112d87b245e213d8bfcaea81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: Date.now()
slug: Web/JavaScript/Reference/Global_Objects/Date/now
tags:
  - Date
  - JavaScript
  - Method
  - polyfill
  - 参考
  - 方法
  - 时间
translation_of: Web/JavaScript/Reference/Global_Objects/Date/now
---
<div>{{JSRef}}</div>

<p><strong><code>Date.now()</code></strong> 方法返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。</p>

<div>{{EmbedInteractiveExample("pages/js/date-now.html")}}</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var timeInMs = Date.now();
</pre>

<h3 id="返回值">返回值</h3>

<p>一个 {{jsxref("Number")}},表示自 UNIX 纪元开始(1970 年 1 月 1 日 00:00:00 (UTC))到当前时间的毫秒数。</p>

<h2 id="描述">描述</h2>

<p>因为 <code>now()</code>{{jsxref("Date")}} 的一个静态函数,所以必须以 <code>Date.now()</code> 的形式来使用。</p>

<h2 id="时间精度被降低"><span class="tlid-translation translation" lang="zh-CN"><span title="">时间精度被降低</span></span></h2>

<p><span class="tlid-translation translation" lang="zh-CN"><span title="">为了提供针对定时攻击和指纹追踪的保护,</span></span><code>Date.now()</code> <span class="tlid-translation translation" lang="zh-CN"><span title="">的精度可能会根据浏览器的高级设置项目而被取整。</span></span><br>
 在 Firefox 中,默认启用 <code>privacy.reduceTimerPrecision</code> 设置项,在 Firefox 59 中,默认被取整至 20 微秒;在 Firefox 60 中,则被取整至 2 毫秒。</p>

<pre class="brush: js">// reduced time precision (2ms) in Firefox 60
Date.now()
// 1519211809934
// 1519211810362
// 1519211811670
// ...


// reduced time precision with `privacy.resistFingerprinting` enabled
Date.now();
// 1519129853500
// 1519129858900
// 1519129864400
// ...
</pre>

<p>在 Firefox 中,还可以通过启用 <code>privacy.resistFingerprinting</code> 来进一步降低精度。启用后,精度将为 100 毫秒或者 <code>privacy.resistFingerprinting.reduceTimerPrecision.microseconds</code> 的值,取决于这两个值中哪一个更大,也就是,精度更低一些。</p>

<h2 id="Polyfill">Polyfill</h2>

<p>该方法在 ECMA-262 第五版中被标准化,可以通过下面的代码端来兼容那些不支持该方法的引擎:</p>

<pre class="brush: js">if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">说明</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.9.4.4', 'Date.now')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>初始定义。在 JavaScript 1.5 中实现</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-date.now', 'Date.now')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> <br>
     </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("javascript.builtins.Date.now")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{domxref("Performance.now()")}} — 提供了精确到亚毫秒(sub-millisecond)的时间戳,用于衡量网页性能。</li>
 <li>{{domxref("console.time")}} / {{domxref("console.timeEnd")}}</li>
</ul>