Skip to content

getRelativeDay()

getRelativeDay(targetDate, options?): string

Defined in: utils/date/getRelativeDay.ts:83

Get a human-friendly description of a date relative to "today".

The output is intentionally simple and English-only:

  • "Today"
  • "Yesterday"
  • "Tomorrow"
  • "Last Friday"
  • "Next Monday"
  • "2 weeks ago Tuesday"
  • "In 3 weeks Sunday"

The comparison is done in UTC days, so it is not affected by the local time-of-day differences or daylight saving transitions.

Parameters

targetDate

The date to compare with "today". Accepts:

  • Date instance
  • ISO-like string (passed to new Date(...))
  • timestamp number (milliseconds since epoch)
  • null / undefined → treated as invalid and returns ""

string | number | Date | null | undefined

options?

GetRelativeDayOptions = {}

Additional configuration:

  • now: Reference "today" as Date or timestamp. Defaults to new Date().
  • weekdays: Custom weekday labels (length 7, starting from Sunday).

Returns

string

A relative day description, or an empty string ("") when the date cannot be parsed.

Example

ts
getRelativeDay(new Date());                  // "Today"
getRelativeDay('2024-01-01');                // e.g. "Last Monday"
getRelativeDay(Date.now() - 7 * 86400000);   // "Last <weekday>"
getRelativeDay(Date.now() - 14 * 86400000);  // "2 weeks ago <weekday>"

Released under the MIT License.