Last active
March 9, 2020 02:01
-
-
Save black-black-cat/ddc575d1bf7265e0f1b231560df390ba to your computer and use it in GitHub Desktop.
format time, format duration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const padNum = require('padNum') | |
const formatTime = date => { | |
const year = date.getFullYear() | |
const month = date.getMonth() + 1 | |
const day = date.getDate() | |
const hour = date.getHours() | |
const minute = date.getMinutes() | |
const second = date.getSeconds() | |
return [year, month, day].map(padNum).join('/') + ' ' + [hour, minute, second].map(padNum).join(':') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const padNum = require('padNum'); | |
let parseMs = ms => { | |
if (typeof ms !== 'number') { | |
throw new TypeError('Expected a number'); | |
} | |
const roundTowardsZero = ms > 0 ? Math.floor : Math.ceil; | |
return { | |
hours: roundTowardsZero(ms / 3600000), | |
minutes: roundTowardsZero(ms / 60000) % 60, | |
seconds: roundTowardsZero(ms / 1000) % 60, | |
milliseconds: roundTowardsZero(ms) % 1000 | |
}; | |
}; | |
let format = (obj = {}, shouldPad = true) => { | |
let arr = []; | |
arr = Object.keys(obj).slice(0, 3).map(key => { | |
return shouldPad ? padNum(obj[key]) : '' + obj[key]; | |
}); | |
return obj.milliseconds ? arr.join(':') + '.' + obj.milliseconds : arr.join(':') | |
} | |
let formatDuration = (ms) => { | |
return format(parseMs(ms)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getMeridiemSuffixOfInteger = num => | |
num === 0 || num === 24 | |
? { clockHour: 12, suffix: 'am' } | |
: num === 12 | |
? { clockHour: 12, suffix: 'pm' } | |
: num < 12 | |
? { clockHour: (num % 12), suffix: 'am' } | |
: { clockHour: (num % 12), suffix: 'pm' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>GistRun</title> | |
<!--<link rel="stylesheet" href="styles.css">--> | |
</head> | |
<body> | |
<h1>Hello world!</h1> | |
<script src="padNum.js"></script> | |
<script src="formatDuration.js"></script> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function padNum (n) { | |
n = '' + n; | |
let width = 2 - n.length; | |
return width > 0 ? Array(width + 1).join('0') + n : n; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HOUR_MS = 3600000 | |
const MINUTE_MS = 60000 | |
const SECOND_MS = 1000 | |
let parseMs = ms => { | |
if (typeof ms !== 'number') { | |
throw new TypeError('Expected a number'); | |
} | |
const roundTowardsZero = ms > 0 ? Math.floor : Math.ceil; | |
return { | |
hours: roundTowardsZero(ms / HOUR_MS), | |
minutes: roundTowardsZero(ms % HOUR_MS / MINUTE_MS), | |
seconds: roundTowardsZero(ms % MINUTE_MS / SECOND_MS), | |
milliseconds: roundTowardsZero(ms % 1000) | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function humanToMs (str = '00:00:00') { | |
let parts = str.split(':') | |
let secParts = parts[2].split('.') | |
parts[2] = secParts[0] | |
parts[3] = secParts[1] || 0 | |
const msMap = { | |
h: 60 * 60 * 1000, | |
m: 60 * 1000, | |
s: 1000, | |
ms: 1 | |
} | |
let keys = Object.keys(msMap) | |
return parts.reduce((memo, item, i) => { | |
return memo + (item - 0) * msMap[keys[i]] | |
}, 0) | |
} | |
let d = humanToMs('19:02:13.333') | |
console.log(d) | |
console.log(formatDuration(d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment