Skip to main content
Version: 11.14.0

Date Utilities

This module provides utility functions for comparing dates and generating localized relative time strings such as "in 2 days" or "4 days ago". It uses the browser's Intl.RelativeTimeFormat API.

Types

RelativeDate

Represents the computed difference between two dates.

type RelativeDate = {
offset: number;
unit: Intl.RelativeTimeFormatUnit;
isFuture: boolean;
};
PropertyTypeDescription
offsetnumberAbsolute difference in days between the two dates
unitIntl.RelativeTimeFormatUnitAlways 'day' in the current implementation
isFuturebooleantrue if the target date is in the future

Functions

getRelativeDate()

Returns a localized, human-readable relative date string.

function getRelativeDate(locale: string, target: string, base?: string): string

Parameters

ParameterTypeRequiredDescription
localestringBCP 47 locale code (e.g. 'en-US', 'fr-FR')
targetstringTarget date as an ISO string
basestringBase date to compare against. Default: current date

Returns string — localized relative time string (e.g. "in 2 days", "4 days ago").

Example

get-relative-date.ts
import { getRelativeDate } from '@sinequa/atomic';

const futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 2);
getRelativeDate('en-US', futureDate.toISOString()); // 'in 2 days'

getRelativeDate('en-US', '2025-04-25T10:00:00Z', '2025-04-29T10:00:00Z'); // '4 days ago'
getRelativeDate('fr-FR', '2025-04-25T10:00:00Z', '2025-04-29T10:00:00Z'); // 'il y a 4 jours'

getOffsetFromDates()

Calculates the date difference and returns a structured RelativeDate object.

function getOffsetFromDates(target: string, base?: string): RelativeDate

Parameters

ParameterTypeRequiredDescription
targetstringTarget date as an ISO string
basestringBase date to compare against. Default: current date

Returns RelativeDate

Example

get-offset-from-dates.ts
import { getOffsetFromDates } from '@sinequa/atomic';

const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
getOffsetFromDates(tomorrow.toISOString());
// { offset: 1, unit: 'day', isFuture: true }

getOffsetFromDates('2025-04-15T10:00:00Z', '2025-04-29T10:00:00Z');
// { offset: 14, unit: 'day', isFuture: false }
Limitations

The current implementation always returns differences in days, regardless of the actual time span. The unit property is always 'day'.