Skip to content

A self-hosted media server and file management system

add Timespan util function

aminnausinb5ead0e43c5e
diff --git a/resources/js/service/util.js b/resources/js/service/util.js
index 0b6f108f..b7f38cb4 100644
--- a/resources/js/service/util.js
+++ b/resources/js/service/util.js
@@ -2,4 +2,18 @@ export function toTitleCase(str) {
return str.toLowerCase().replace(/(?:^|\s)\w/g, function(match) {
return match.toUpperCase();
});
+}
+
+export function toTimeSpan(rawDate) {
+ const rawAge = Date.now() - rawDate.getTime();
+
+ const weeks = Math.round(rawAge / (1000 * 3600 * 24 * 7));
+ const days = Math.round(rawAge / (1000 * 3600 * 24));
+ const hours = Math.round(rawAge / (1000 * 3600));
+ const minutes = Math.round(rawAge / (1000 * 60));
+ const seconds = Math.round(rawAge / (1000));
+
+ const timeSpan = weeks > 0 ? `${weeks} week${weeks > 1 ? 's' : ''} ago` : days > 0 ? `${days} day${days > 1 ? 's' : ''} ago` : hours > 0 ? `${hours} hour${hours > 1 ? 's' : ''} ago` : minutes > 0 ? `${minutes}m ago` : `${seconds}s ago`
+
+ return timeSpan;
}
\ No newline at end of file