3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【JS】経過時間を取得する

Last updated at Posted at 2020-07-21

経過時間を知りたい

どれくらい時間が経過したのか知りたいときに便利です。

.js
const startTime = Date.now();
let nowTime = 0;

nowTime = (Date.now() - startTime) / 1000;

Date.now()

UTC (協定世界時) での 1970 年 1 月 1 日 0 時 0 分 0 秒 から現在までの経過時間をミリ秒単位で返します。

参考
Date.now()

なので、1563292043074 みたいな数字が返ってきます。

startTimeDate.now() が格納された時間から、今この瞬間のDate.now() を引くと経過時間のミリ秒がわかります。

求められたミリ秒を1000で割ると秒数を求めることができます。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?