Aller au contenu
GitHub

API JavaScript

@mdgascar/date

Ce contenu n’est pas encore disponible dans votre langue.

Malagasy dates: month and weekday names, and dates written in Malagasy. Part of mdg-utils by Mdgascar.

Error thrown by mdg-utils functions. Check code, which is the same in every language.

try {
numberToWords(-1);
} catch (error) {
if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
// handle it
}
}
  • Error

new MdgError(code, message): MdgError

Creates an error with a shared spec code.

ErrorCode

Shared spec error code.

string

Human-readable explanation, in English.

MdgError

Error.constructor

readonly code: ErrorCode

Why the call failed; the same code in every language.

readonly name: "MdgError" = 'MdgError'

Always 'MdgError'.

Error.name

static [hasInstance](value): boolean

Makes instanceof MdgError true for errors from any mdg-utils package or build.

unknown

Any value, usually a caught error.

boolean

true when value is an mdg-utils error.

Each package, and its ESM and CommonJS builds, has its own copy of this class, so the check looks at the error’s shape instead of its prototype chain.

A calendar date: year 1–9999, month 1–12, day of the month.

const independenceDay: CalendarDate = { year: 1960, month: 6, day: 26 };

readonly day: number

Day of the month, 1 to 31.

readonly month: number

Month, 1 (Janoary) to 12 (Desambra).

readonly year: number

Year, 1 to 9999.

DateStyle = "long" | "full" | "medium" | "short" | "numeric"

Date style for formatDate; long is the default.


ErrorCode = "INVALID_TYPE" | "OUT_OF_RANGE" | "INVALID_FORMAT" | "UNKNOWN_OPERATOR"

Error code shared by every mdg-utils language port.

  • INVALID_TYPE: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
  • OUT_OF_RANGE: The input has the right type but lies outside the supported range.
  • INVALID_FORMAT: The input text cannot be read as a value of this module (wrong characters or wrong length).
  • UNKNOWN_OPERATOR: The phone number is well formed but its operator code is not assigned to a mobile operator.

NameWidth = "wide" | "abbreviated"

Name width: wide (Novambra, default) or abbreviated (Nov).

const width: NameWidth = 'abbreviated';

const SPEC_VERSION: "0.6.0" = '0.6.0'

Version of the shared spec this code was generated from.

formatDate(date, style?): string

Writes a date in Malagasy.

string | CalendarDate | Date

YYYY-MM-DD text, { year, month, day }, or a Date (read in its local calendar fields).

DateStyle = DEFAULT_STYLE

long (default), full, medium, short (CLDR) or numeric (dd/MM/y).

string

The formatted date.

MdgError INVALID_FORMAT for text that is not a real YYYY-MM-DD date, OUT_OF_RANGE for impossible fields or years outside 1–9999, INVALID_TYPE for other input or an unknown style.

formatDate('2026-11-23'); // '23 Novambra 2026'
formatDate('2026-11-23', 'full'); // 'Alatsinainy 23 Novambra 2026'
formatDate({ year: 1960, month: 6, day: 26 }, 'numeric'); // '26/06/1960'

getMonthName(month, width?): string

The Malagasy name of a month.

number

Month number, 1 (Janoary) to 12 (Desambra).

NameWidth = 'wide'

wide (default) or abbreviated.

string

The month name.

MdgError OUT_OF_RANGE outside 1–12, INVALID_TYPE for non-integers or an unknown width.

getMonthName(6); // 'Jona'
getMonthName(11, 'abbreviated'); // 'Nov'

getWeekdayName(isoWeekday, width?): string

The Malagasy name of a weekday, numbered as in ISO 8601 (Monday 1 … Sunday 7).

number

1 (Alatsinainy) to 7 (Alahady).

NameWidth = 'wide'

wide (default) or abbreviated.

string

The weekday name.

MdgError OUT_OF_RANGE outside 1–7, INVALID_TYPE for non-integers or an unknown width.

getWeekdayName(1); // 'Alatsinainy'
getWeekdayName(7, 'abbreviated'); // 'Alah'