blob: 8f6d1833adcfc904cad578e606be188ff46388b9 (
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
100
101
102
103
104
105
106
107
108
|
---
title: Date.prototype.getTime()
slug: Web/JavaScript/Reference/Global_Objects/Date/getTime
tags:
- Date
- JavaScript
- Method
- Prototype
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Date/getTime
---
<div>{{JSRef}}</div>
<p><strong><code>getTime()</code></strong> メソッドは、 <a href="https://ja.wikipedia.org/wiki/UNIX%E6%99%82%E9%96%93">UNIX 元期</a>からの経過ミリ秒数*を返します。</p>
<p>* JavaScriptは測定単位として<em>ミリ秒</em>を使用しますが、 Unix 時間は<em>秒</em>単位です。</p>
<p><em>getTime() は時間表現に常に UTC を使用します。例えば、あるタイムゾーンに設定されているブラウザーの getTime() も、他のタイムゾーンに設定されたブラウザーの getTime() も、同じ値を返します。</em></p>
<p>このメソッドは、日付と時刻を別の {{jsxref("Date")}} オブジェクトへ代入する助けとして使用できます。このメソッドは、{{jsxref("Date.valueof", "valueOf()")}} メソッドと機能的に同等です。</p>
<div>{{EmbedInteractiveExample("pages/js/date-gettime.html","shorter")}}</div>
<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate"><code><var>dateObj</var>.getTime()</code></pre>
<h3 id="Return_value" name="Return_value">返値</h3>
<p>1970 年 1 月 1 日 00:00:00 UTC から指定した日時までの経過時間をミリ秒で表した数値。</p>
<h2 id="Reduced_time_precision" name="Reduced_time_precision">時刻の低精度化</h2>
<p>ブラウザーの設定によっては、タイミング攻撃と Fingerprinting に対する保護を提供するために <code>new Date().getTime()</code> の精度が落とされていることがあります。 Firefox では、<code>privacy.reduceTimerPrecision</code> が既定で有効になっています。既定値は Firefox 59 では 20 マイクロ秒で、 Firefox 60 では 2 ミリ秒です。</p>
<pre class="brush: js notranslate">// Firefox 60 での時刻の精度の低下(2 ミリ秒)
new Date().getTime();
// 1519211809934
// 1519211810362
// 1519211811670
// ...
// `privacy.resistFingerprinting` が有効な場合の時刻の精度の低下
new Date().getTime();
// 1519129853500
// 1519129858900
// 1519129864400
// ...
</pre>
<p>Firefox では、 <code>privacy.resistFingerprinting</code> も有効にできます。この場合、精度は 100 ミリ秒と <code>privacy.resistFingerprinting.reduceTimerPrecision.microseconds</code> 値のうち大きい方になります。</p>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Using_getTime_for_copying_dates" name="Using_getTime_for_copying_dates">getTime() を使って日付をコピーする</h3>
<p>同一の time 値を持つ date オブジェクトを構築します。</p>
<pre class="brush: js notranslate">// month は 0 を基点とするため、birthday は 1995 年 1 月 10 日になります
var birthday = new Date(1994, 12, 10);
var copy = new Date();
copy.setTime(birthday.getTime());
</pre>
<h3 id="Measuring_execution_time" name="Measuring_execution_time">実行時間を計測する</h3>
<p>新たに生成された {{jsxref("Date")}} オブジェクトでの、続く 2 個の <code>getTime()</code> の結果を減算して、これらの呼び出しと呼び出しの間の時間を得ます。これは、何らかの命令の実行時間を計測するために使用できます。不要な {{jsxref("Date")}} オブジェクトのインスタンス化を避けるため、{{jsxref("Date.now()")}} も参照してください。</p>
<pre class="brush: js notranslate">var end, start;
start = new Date();
for (var i = 0; i < 1000; i++) {
Math.sqrt(i);
}
end = new Date();
console.log('Operation took ' + (end.getTime() - start.getTime()) + ' msec');
</pre>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-date.prototype.gettime', 'Date.prototype.getTime')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<p>{{Compat("javascript.builtins.Date.getTime")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{jsxref("Date.prototype.setTime()")}}</li>
<li>{{jsxref("Date.prototype.valueOf()")}}</li>
<li>{{jsxref("Date.now()")}}</li>
</ul>
|