Time
import Time from "@arclockproject/common/library/Time";
For examples and live demos, see the Time guide.
Time
Object · default export
Helpers for showing durations and elapsed time as text.
Time.parseTimeDuration()
Time.parseTimeDuration(time: number | string): string | null
Formats seconds as a timestamp, like a video player shows: 1:00:05, 2:00 or 0:06.
Hours are left out when they are 0, and the first unit is not zero-padded.
356000 means "unknown duration" and gives "--:--".
Parameters
time(number | string) — Seconds. A string is read withparseInt(), so decimals are dropped.
Returns string | null — H:MM:SS or M:SS. "--:--" for 356000, and null if time is not a number.
Example
Time.parseTimeDuration(3605); // "1:00:05"
Time.parseTimeDuration(120); // "2:00"
Time.parseTimeDuration(6); // "0:06"
Time.parseTimeDuration("foo"); // null
Time.parseTimeDuration(356000); // "--:--"
Time.timeSince()
Time.timeSince(date: number | string): string
Says how long ago date was, in the largest whole unit, e.g. "2 days".
Add your own "ago" when showing it.
A unit is only used once more than 1 of it has passed, so exactly one hour
gives "60 minutes". Units are always plural, e.g. "1 hours". A month
counts as 30 days and a year as 365 days.
Parameters
date(number | string) — Unix timestamp in seconds, not milliseconds. A string is read withparseInt().
Returns string — "{number} {unit}", where unit is minutes, hours, days, months or years.
Example
const now = Math.floor(Date.now() / 1000);
Time.timeSince(now - 130); // "2 minutes"
Time.timeSince(now - 3600); // "60 minutes"
Time.timeSince(now - 3601); // "1 hours"
Time.timeSince(now - 172800); // "2 days"