Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active July 13, 2019 06:28
Show Gist options
  • Save black-black-cat/956293ccabc29cf97893edb269a98429 to your computer and use it in GitHub Desktop.
Save black-black-cat/956293ccabc29cf97893edb269a98429 to your computer and use it in GitHub Desktop.
parse ms
let floor = Math.floor.bind(null)
let factors = [1, 1000, 1000 * 60, 1000 * 60 * 60, 1000 * 60 * 60 * 24]
function parseMs (input, {withDay = false}) {
let ms = input % 1000
let { h, m, s } = parseSec(floor(input / 1000))
if (withDay) {
let d = floor(h / 24)
h = h % 24
return {
d, h, m, s, ms
}
}
return {
h, m, s, ms
}
}
function parseSec(input) {
let s = input % 60
let h = floor(input / 60 / 60)
let m = floor(input / 60 % 60)
return {
h, m, s
}
}
let ms = toMs({
h: 36,
// h,
// m,
// s,
ms: 999
})
console.log('ms', ms)
let parsed = parseMs(ms)
console.log('parsed', parsed)
function toMs ({
d = 0,
h = 0,
m = 0,
s = 0,
ms = 0
}) {
return [ms, s, m, h, d].reduce((memo, v, i) => {
return memo + v * factors[i]
}, 0)
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1></h1>
<script src="script.js"></script>
</body>
</html>
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment