LoginSignup
22
16

More than 1 year has passed since last update.

JavaScript で秒や分を別の時間単位に変換する

Last updated at Posted at 2017-08-24

※ 2022年2月10日更新 - 流石に今どきグローバルにオブジェクトを登録するのもどうかと思ったのでClassに変更しました

例えば 1920 秒は何分何秒なのか、何時間何分何秒なのかといった計算をする場合に使う JavaScript を書きました。

まったくもって難しいことは何も無いんですが、意外と情報が見つからないんで。

検索キーワード難しいですよね。

class TimeConvert {
    static sec2min(time) {
        var min = Math.floor(time / 60);
        var sec = time % 60;

        return {
            min: min,
            sec: sec
        }
    }

    static min2hour(time) {
        var hour = Math.floor(time / 60);
        var min = time % 60;

        return {
            hour: hour,
            min: min
        }
    }

    static sec2hour(time) {
        var sec = time % 60;
        var min = Math.floor(time / 60) % 60;
        var hour = Math.floor(time / 3600);

        return {
            hour: hour,
            min: min,
            sec: sec
        }
    }
}

デモを CodePen に置いておきます。

See the Pen Time Convert by Shingo Matsui (@shingorow) on CodePen.

22
16
2

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
22
16