Date.now()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Date.now() メソッドは、UTC (協定世界時) での 1970 年 1 月 1 日 0 時 0 分 0 秒 から現在までの経過時間をミリ秒単位で返します。

試してみましょう

// This example takes 2 seconds to run
const start = Date.now();

console.log("starting timer...");
// Expected output: "starting timer..."

setTimeout(() => {
  const millis = Date.now() - start;

  console.log(`seconds elapsed = ${Math.floor(millis / 1000)}`);
  // Expected output: "seconds elapsed = 2"
}, 2000);

構文

js
Date.now()

返値

ECMAScript 元期からの経過時間をミリ秒単位で表す数値です。

時刻の低精度化

ブラウザーの設定によっては、タイミング攻撃とフィンガープリンティングに対する保護を提供するために Date.now() の精度が落とされていることがあります。 Firefox では、privacy.reduceTimerPrecision が既定で有効になっています。既定値は Firefox 59 では 20 マイクロ秒で、 Firefox 60 では 2 ミリ秒です。

js
// Firefox 60 での時刻の精度の低下(2 ミリ秒)
Date.now();
// 1519211809934
// 1519211810362
// 1519211811670
// …

// `privacy.resistFingerprinting` が有効な場合の時刻の精度の低下
Date.now();
// 1519129853500
// 1519129858900
// 1519129864400
// …

Firefox では、 privacy.resistFingerprinting も有効にできます。この場合、精度は 100 ミリ秒と privacy.resistFingerprinting.reduceTimerPrecision.microseconds 値のうち大きい方になります。

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-date.now

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
now

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報