blob: 3c7f4ca209ab9dc138bf1d04791a749339efaa4b (
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
|
---
title: Date.now()
slug: Web/JavaScript/Reference/Global_Objects/Date/now
tags:
- Date
- JavaScript
- Method
- Reference
- polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Date/now
---
<div>{{JSRef}}</div>
<p><strong><code>Date.now()</code></strong> メソッドは、UTC (協定世界時) での 1970 年 1 月 1 日 0 時 0 分 0 秒 から現在までの経過時間をミリ秒単位で返します。</p>
<div>{{EmbedInteractiveExample("pages/js/date-now.html")}}</div>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate">var <var>timeInMs</var> = Date.now();</pre>
<h3 id="Return_value">Return value</h3>
<p>UNIX 元期からの経過時間をミリ秒単位で表す{{jsxref("Number", "数値")}}。</p>
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
<p>このメソッドは ECMA-262 第 5 版で標準化されました。このメソッドに対応するよう更新されていないエンジンでは、次の互換コードを使用することにより、実装の欠落を補うことができます。</p>
<pre class="brush: js notranslate">if (!Date.now) {
Date.now = function now() {
return new Date().getTime();
};
}
</pre>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Reduced_time_precision" name="Reduced_time_precision">時刻の低精度化</h3>
<p>ブラウザーの設定によっては、タイミング攻撃と Fingerprinting に対する保護を提供するために <code>new Date().getTime()</code> の精度が落とされていることがあります。<br>
Firefox では、<code>privacy.reduceTimerPrecision</code> が既定で有効になっています。既定値は Firefox 59 では 20 マイクロ秒で、 Firefox 60 では 2 ミリ秒です。</p>
<pre class="brush: js notranslate">// Firefox 60 での時刻の精度の低下(2 ミリ秒)
Date.now()
// 1519211809934
// 1519211810362
// 1519211811670
// ...
// `privacy.resistFingerprinting` が有効な場合の時刻の精度の低下
Date.now();
// 1519129853500
// 1519129858900
// 1519129864400
// ...
</pre>
<p>Firefox では、 <code>privacy.resistFingerprinting</code> も有効にできます。この場合、精度は 100 ミリ秒と <code>privacy.resistFingerprinting.reduceTimerPrecision.microseconds</code> 値のうち大きい方になります。</p>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">仕様書</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-date.now', 'Date.now')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<p>{{Compat("javascript.builtins.Date.now")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{domxref("Performance.now()")}} — ウェブページのパフォーマンス測定のための、ミリ秒以下の分解能を持つタイムスタンプを提供</li>
<li>{{domxref("console.time()")}} / {{domxref("console.timeEnd()")}}</li>
</ul>
|