useDate
Formats dates and "2 hours ago" style relative times in the page language, using the browser or server Intl APIs.
import { useDate, UseDateResult } from '@salla.sa/twilight-theme-engine/hooks';In plain words
Dates from the Salla API are strings like "2024-12-31 10:30:00". useDate() turns them into something a shopper reads: format(date) gives "December 31, 2024" on English pages and the Arabic equivalent on Arabic pages, and ago(date) gives "2 hours ago".
now is the current date and time, read when your component renders.
Signature
function useDate(): UseDateResult
interface UseDateResult {
format: (date: DateInput, formatType?: 'full' | 'long' | 'medium' | 'short' | 'time') => string;
ago: (date: DateInput) => string;
now: Date;
}
// Not exported:
type DateInput =
| string | number | Date
| { date: string; timezone_type: number; timezone: string }
| undefined;Try it live
import { useDate, useIsClient } from '@salla.sa/twilight-theme-engine/hooks';
export function PostDate({ publishedAt = '2024-12-31T10:30:00' }: { publishedAt?: string }) {
const { format, ago } = useDate();
const isClient = useIsClient(); // relative time depends on the clock: render it after hydration
return (
<p>
<time dateTime={publishedAt}>{format(publishedAt)}</time>
{isClient && <span> · {ago(publishedAt)}</span>}
</p>
);
}
Example
import { useDate, useIsClient } from '@salla.sa/twilight-theme-engine/hooks';
export function PostDate({ publishedAt }: { publishedAt: string }) {
const { format, ago } = useDate();
const isClient = useIsClient();
return (
<p className="post-date">
<time dateTime={publishedAt}>{format(publishedAt, 'long')}</time>
{/* "ago" depends on the clock, so render it only in the browser */}
{isClient && <span> · {ago(publishedAt)}</span>}
</p>
);
}
How it behaves
The language is
useTranslation().locale.formatusesIntl.DateTimeFormat;agousesIntl.RelativeTimeFormatwithnumeric: auto, so one day back reads "yesterday".The format types:
fulladds the weekday,medium(the default) andlongare identical (day, long month, year),shortuses the short month name, andtimeis a 2-digit hour and minute only.agorounds down into year (365 days), month (30 days), week, day, hour, minute and second. Anything under a second returns "just now", or "الآن" for anarlocale.A
{ date, timezone_type, timezone }object (the PHP date shape some Salla responses use) is read fromdateonly;timezoneis ignored.It has no subpath of its own: import it from
@salla.sa/twilight-theme-engine/hooks.DateInputis not exported at all.
Gotchas
format(undefined)andformat('')return today's date, not an empty string: an empty input becomesnew Date(). Check for a missing date before calling it.Future dates return "just now" from
ago: only past differences match a unit, so "in 3 days" never appears.agoandnowread the clock while rendering, and the server renders in UTC while the browser uses local time. The server HTML and the first browser render can disagree (a hydration mismatch). Render relative times after mount, withuseIsClient().An unparseable string makes
Intlthrow;formatcatches that and returnstoLocaleDateString(), which is the text "Invalid Date".docs/18-hooks-api.md imports it from
@salla.sa/twilight-theme-engine/hooks/useDate, a subpath that does not exist. Import from@salla.sa/twilight-theme-engine/hooks.