diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/javascript/reference/global_objects/date/gettime | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/date/gettime')
-rw-r--r-- | files/ja/web/javascript/reference/global_objects/date/gettime/index.html | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/date/gettime/index.html b/files/ja/web/javascript/reference/global_objects/date/gettime/index.html new file mode 100644 index 0000000000..c0ff3ff0e1 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/date/gettime/index.html @@ -0,0 +1,110 @@ +--- +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 class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</p> + +<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> |