mirror of
https://git.sr.ht/~cadence/cloudtube
synced 2026-05-26 12:32:25 +00:00
Move utils folder and fix published text
This commit is contained in:
parent
643f1e0889
commit
2e69dfc4b7
16 changed files with 53 additions and 619 deletions
21
utils/converters.js
Normal file
21
utils/converters.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
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"
|
||||
}
|
||||
|
||||
module.exports.timeToPastText = timeToPastText
|
||||
Loading…
Add table
Add a link
Reference in a new issue