MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / formatSeconds

Function formatSeconds

scripts/web_timer_main.js:458–481  ·  view source on GitHub ↗
(sec, keepAll = false)

Source from the content-addressed store, hash-verified

456}
457
458function formatSeconds(sec, keepAll = false) {
459 if (FormatCache.has(sec)) return FormatCache.get(sec);
460 let days = Math.floor(sec / 86400);
461 let hours = Math.floor((sec % 86400) / 3600);
462 let minutes = Math.floor((sec % 3600) / 60);
463 let seconds = Math.floor(sec % 60);
464
465 let result = "";
466 let arr = [
467 [days, "d"],
468 [hours, "h"],
469 [minutes, "m"],
470 [seconds, "s"],
471 ];
472 arr.forEach(([value, unit], index) => {
473 if (value > 0 || keepAll) {
474 if (value < 10) value = "0" + value;
475 result += `${value}${unit}`;
476 if (index < arr.length - 1) result += " ";
477 }
478 });
479 FormatCache.set(sec, result);
480 return result;
481}
482
483function formatDate(date) {
484 let year = date.getFullYear();

Callers 2

showDataFunction · 0.85
DonutFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected