LoginSignup
2
3

More than 5 years have passed since last update.

JavaScriptで時間を曖昧に表す

Last updated at Posted at 2017-05-13

概要

時間を曖昧に変換する

  • 3分45秒 => 4分
  • 28分45秒 => 25分
  • 2時間25分45秒 => 2時間
  • 3日2時間25分45秒 => 3日

ソース

function count(ms) {
    let unit = ["","時間","","",""],
            vari = [86400000,3600000,60000,60000],
            inte = 1,
            leve = 4;
    if(ms>1000*60      ) {inte=1000*60;leve=3}
    if(ms>1000*60*10   ) {inte=1000*60*5;leve=2}
    if(ms>1000*60*60   ) {inte=1000*60*60;leve=1}
    if(ms>1000*60*60*24) {inte=1000*60*60*24;leve=0}
    return (Math.round(ms/inte)*inte)/vari[leve]+unit[leve]
}

使い方

引数にミリ秒を入れるだけ

2
3
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
2
3