0
1

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 5 years have passed since last update.

JS > Moment > 開始と終了の時間の差分を得る

Posted at
let start = moment("2019/05/04 8:00:00");
let goal = moment("2019/05/05 16:43:26");
console.log("start", start);
console.log("goal", goal);

if (start && goal) {
    let duration = moment.duration(goal.diff(start), 'milliseconds'); //差分をミリ秒で取得し、ミリ秒→durationへ変換
    let hour = Math.floor(duration.asHours());
    let time = hour + ":" + duration.minutes() + ":" + duration.seconds();
    console.log("time", time);
}

参考

javascript - Convert milliseconds to hours and minutes using Momentjs - Stack Overflow

参考

以下はStackOverflowでも指摘されているが、24時間オーバーの場合には正しく計算できない

let time4 = moment.utc(milliseconds).format('HH:mm');
console.log("time4", time4);
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?