1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-11-10 02:27:29 +00:00
cloudtube/utils/converters.js
2020-10-06 23:43:44 +13:00

32 lines
985 B
JavaScript

function timeToPastText(timestamp) {
const difference = Date.now() - timestamp
return [
["year", 365 * 24 * 60 * 60 * 1000],
["month", 30 * 24 * 60 * 60 * 1000],
["week", 7 * 24 * 60 * 60 * 1000],
["day", 24 * 60 * 60 * 1000],
["hour", 60 * 60 * 1000],
["minute", 60 * 1000],
["second", 1 * 1000]
].reduce((acc, [unitName, unitValue]) => {
if (acc) return acc
if (difference > unitValue) {
const number = Math.floor(difference / unitValue)
const pluralUnit = unitName + (number == 1 ? "" : "s")
return `${number} ${pluralUnit} ago`
}
}, null) || "just now"
}
function lengthSecondsToLengthText(seconds) {
return [Math.floor(seconds/3600), Math.floor(seconds/60)%60, seconds%60]
.reduce((a, c, i, t) => (
a ? a : c || i == 1 ? t.slice(i) : false
), false)
.map((x, i) => i === 0 ? x : (x+"").padStart(2, "0"))
.join(":")
}
module.exports.timeToPastText = timeToPastText
module.exports.lengthSecondsToLengthText = lengthSecondsToLengthText