Skip to main content
Glama
useRelativeTime.ts1.11 kB
'use client'; import { relativeTime } from '@intlayer/core'; import { useCallback, useContext } from 'preact/hooks'; import { IntlayerClientContext } from '../IntlayerProvider'; /** * Client-side React hook for accessing a localized relative time formatter. * * This hook: * - Reads the current locale from {@link useLocaleBase}. * - Creates a new relative time formatter with {@link createRelativeTime}. * - Returns a function that can format time differences into localized strings. * * Example: * ```tsx * const relativeTime = useRelativeTime(); * const formatted = relativeTime(new Date("2024-08-01"), new Date()); * // e.g., "2 weeks ago" * ``` * * @returns {ReturnType<typeof createRelativeTime>} A relative time formatting function * bound to the current client locale. */ export const useRelativeTime = () => { const { locale } = useContext(IntlayerClientContext); return useCallback( (...args: Parameters<typeof relativeTime>) => relativeTime(args[0], args[1], { ...args[2], locale: args[2]?.locale ?? locale, }), [locale] ); };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aymericzip/intlayer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server