diff options
author | tdro <tdro@noreply.example.com> | 2022-07-29 21:24:09 -0400 |
---|---|---|
committer | tdro <tdro@noreply.example.com> | 2022-07-29 21:24:09 -0400 |
commit | b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e (patch) | |
tree | 984d46e6ddd5edbddc4b0735752c2982c8d36f1c /assets/js | |
parent | cc0f3566fc82fd8bfdeb6d5e4234f052fcd764e9 (diff) | |
download | canory-b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e.tar.gz canory-b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e.tar.bz2 canory-b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e.zip |
static/js: Add relative dates
Diffstat (limited to 'assets/js')
-rw-r--r-- | assets/js/index.js | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/assets/js/index.js b/assets/js/index.js index d64997c..71ff972 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -434,4 +434,46 @@ } }); })(); -console.log("Surface Control: OK"); +(function() { + const relative = new Intl.RelativeTimeFormat("en", { + localeMatcher: "best fit", + numeric: "always", + style: "long" + }); + const date = ()=>{ + [ + ...document.querySelectorAll("time") + ].forEach((element)=>{ + try { + const time = new Date(element.dateTime) || new Date(); + const interval = (new Date().getTime() - time.getTime()) / 1000; + const seconds = Math.floor(interval); + const minutes = Math.floor(seconds / 60); + const hours = Math.floor(minutes / 60); + const days = Math.floor(hours / 24); + if (seconds <= 60) { + return element.textContent = relative.format(-1 * seconds, "second"); + } + if (minutes <= 120) { + return element.textContent = relative.format(-1 * minutes, "minute"); + } + if (hours <= 48) { + return element.textContent = relative.format(-1 * hours, "hour"); + } + if (days <= 60) { + return element.textContent = relative.format(-1 * days, "day"); + } + } catch (err) { + console.error("Error: Unable to resolve relative time format!", err); + } + }); + }; + const substitution = setInterval(date, 4); + self.addEventListener("load", ()=>{ + setTimeout(()=>{ + clearInterval(substitution); + setInterval(date, 1000); + }, 1000); + }); +})(); +console.log("Surface Control: Complete"); |