Skip to content

getRelativeDay()

getRelativeDay(targetDate): string

Defined in: src/utils/dateUtils.ts:141

Gets a relative day description based on a given date, e.g., 'Yesterday', 'Last Friday', '2 weeks ago Thursday'.

Parameters

targetDate

The date to compare with today. Can be a Date object or a string.

string | Date

Returns

string

A string representing the relative day. If the date is invalid, returns an empty string.

Example

tsx
const yesterday = getRelativeDay(new Date(Date.now() - 24 * 60 * 60 * 1000));
console.log(yesterday); // Outputs: 'Yesterday'

const lastFriday = getRelativeDay('2024-11-15'); // Assuming today is '2024-11-19'
console.log(lastFriday); // Outputs: 'Last Friday'

const twoWeeksAgo = getRelativeDay(new Date(Date.now() - 14 * 24 * 60 * 60 * 1000));
console.log(twoWeeksAgo); // Outputs: '2 weeks ago Tuesday'

Released under the MIT License.