Display relative time as signal

Hi, I want to display the relative time of a date. I have this file:
const units: { unit: Intl.RelativeTimeFormatUnit; ms: number }[] = [
{ unit: "year", ms: 31536000000 },
{ unit: "month", ms: 2628000000 },
{ unit: "day", ms: 86400000 },
{ unit: "hour", ms: 3600000 },
{ unit: "minute", ms: 60000 },
{ unit: "second", ms: 1000 },
];
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });

/**
* Get language-sensitive relative time message from Dates.
* @param relative - the relative dateTime, generally is in the past or future
* @param pivot - the dateTime of reference, generally is the current time
*/
export function relativeTimeFromDates(relative: Date | null, pivot: Date = new Date()): string {
if (!relative) return "";
const elapsed = relative.getTime() - pivot.getTime();
return relativeTimeFromElapsed(elapsed);
}

/**
* Get language-sensitive relative time message from elapsed time.
* @param elapsed - the elapsed time in milliseconds
*/
export function relativeTimeFromElapsed(elapsed: number): string {
for (const { unit, ms } of units) {
if (Math.abs(elapsed) >= ms || unit === "second") {
return rtf.format(Math.round(elapsed / ms), unit);
}
}
return "";
}
const units: { unit: Intl.RelativeTimeFormatUnit; ms: number }[] = [
{ unit: "year", ms: 31536000000 },
{ unit: "month", ms: 2628000000 },
{ unit: "day", ms: 86400000 },
{ unit: "hour", ms: 3600000 },
{ unit: "minute", ms: 60000 },
{ unit: "second", ms: 1000 },
];
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });

/**
* Get language-sensitive relative time message from Dates.
* @param relative - the relative dateTime, generally is in the past or future
* @param pivot - the dateTime of reference, generally is the current time
*/
export function relativeTimeFromDates(relative: Date | null, pivot: Date = new Date()): string {
if (!relative) return "";
const elapsed = relative.getTime() - pivot.getTime();
return relativeTimeFromElapsed(elapsed);
}

/**
* Get language-sensitive relative time message from elapsed time.
* @param elapsed - the elapsed time in milliseconds
*/
export function relativeTimeFromElapsed(elapsed: number): string {
for (const { unit, ms } of units) {
if (Math.abs(elapsed) >= ms || unit === "second") {
return rtf.format(Math.round(elapsed / ms), unit);
}
}
return "";
}
but if i run the relativeTimeFromDates(new Date(2024, 9, 19, 14, 30)) inside of the html part it does not update when a minute more has passed and the output of the function should change. Solid does not react to this. How can i fix this? Do i need to wrap the function in something? Thanks!
5 Replies
Eve
Eve2mo ago
There's nothing reactive in any of that code, so how would solid react to it?
Greenman999
Greenman999OP2mo ago
How can i make it reactive? Does the date need to be reactive somehow?
Eve
Eve2mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
Eve
Eve2mo ago
Maybe that will get you started. But you really need to read and understand the documentation on how solid works.
Greenman999
Greenman999OP2mo ago
Thank you very much!
Want results from more Discord servers?
Add your server