Module: services/date

Commonly-used functions for rendering badges containing a date

Source:

Methods

(inner) age(date, reversed) → {string}

Determines the color used for a badge according to the age. Age is calculated as days elapsed till current date. The color varies from bright green to red as the age increases or the other way around if reverse is given true.

Parameters:
Name Type Default Description
date Date | string | number | dayjs

JS Date object, string, unix timestamp or dayjs object

reversed boolean false

Reverse the color scale (the older, the better)

Source:
Returns:

Badge color

Type
string

(inner) formatDate(d) → {string}

Returns a formatted date string without the year based on the value of input date param d.

Parameters:
Name Type Description
d Date | string | number | dayjs

JS Date object, string, unix timestamp or dayjs object

Source:
Returns:

Formatted date string

Type
string

(inner) formatRelativeDate(timestamp) → {string}

Returns a relative date from the input timestamp. For example, day after tomorrow's timestamp will return 'in 2 days'.

Parameters:
Name Type Description
timestamp number | string

Unix timestamp

Source:
Returns:

Relative date from the unix timestamp

Type
string

(inner) parseDate(…args) → {dayjs}

Parse and validate a string date into a dayjs object. Use this helper in preference to invoking dayjs directly when parsing a date from string.

Parameters:
Name Type Attributes Description
args any <repeatable>

Variadic: Arguments to pass through to dayjs

Source:
See:
Throws:
  • Error if validation fails
Type
InvalidResponse
Returns:
  • Parsed object
Type
dayjs
Example
parseDate('2024-01-01')
parseDate('31/01/2024', 'DD/MM/YYYY')
parseDate('2018 Enero 15', 'YYYY MMMM DD', 'es')

(inner) renderDateBadge(date, reversed) → {object}

Creates a badge object that displays a date

Parameters:
Name Type Default Description
date Date | string | number | dayjs

JS Date object, string, unix timestamp or dayjs object

reversed boolean false

Reverse the color scale (the older, the better)

Source:
Returns:

A badge object that has two properties: message, and color

Type
object