LoginSignup
0
0

More than 1 year has passed since last update.

if文使いたくない24時間表記の時間の計算

Last updated at Posted at 2022-05-12

if文書きたくないんじゃー

sample.js
let startTime = '22:30'
let elapsedTime = '01:40'
let [sh, sm] = startTime.split(':')
let [eh, em] = elapsedTime.split(':')

let hh = Number(sh) + Number(eh)
let mm = Number(sm) + Number(em)

hh = (hh + Math.floor(mm / 60)) % 24
mm = mm % 60

let rh = ('0' + hh).slice(-2)
let rm = ('0' + mm).slice(-2)

console.log(rh + ':' + rm) // "00:10"

%て割れない数字だとそのまま返してくれるのね。(今更)

0
0
1

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
0