You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/sites/svelte.dev/src/lib/time.js

29 lines
588 B

const formatter = new Intl.RelativeTimeFormat(undefined, {
numeric: 'auto'
});
const DIVISIONS = {
seconds: 60,
minutes: 60,
hours: 24,
days: 7,
weeks: 4.34524,
months: 12,
years: Number.POSITIVE_INFINITY
};
/**
* @param {Date} date
*/
export const ago = (date) => {
let duration = (date.getTime() - new Date().getTime()) / 1000;
for (const [name, amount] of Object.entries(DIVISIONS)) {
if (Math.abs(duration) < amount) {
const format = /** @type {keyof(DIVISIONS)} */ (name);
return formatter.format(Math.round(duration), format);
}
duration /= amount;
}
};