Skip to content
GitHub

JavaScript API

@mdgascar/date-time

Malagasy dates and times: dates written and read in Malagasy, times said in words, month and weekday names. Part of malagasy-intl by Mdgascar.

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

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

new MalagasyIntlError(code, message): MalagasyIntlError

Creates an error with a shared spec code.

ErrorCode

Shared spec error code.

string

Human-readable explanation, in English.

MalagasyIntlError

Error.constructor

readonly code: ErrorCode

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

readonly name: "MalagasyIntlError" = 'MalagasyIntlError'

Always 'MalagasyIntlError'.

Error.name

static [hasInstance](value): boolean

Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.

unknown

Any value, usually a caught error.

boolean

true when value is an malagasy-intl 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.


A date and a time of day: calendar fields plus hour and minute.

const meeting: CalendarDateTime = { year: 2026, month: 11, day: 23, hour: 14, minute: 30 };

readonly day: number

Day of the month, 1 to 31.

CalendarDate.day

readonly hour: number

Hour, 0 to 23.

ClockTime.hour

readonly minute: number

Minute, 0 to 59.

ClockTime.minute

readonly month: number

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

CalendarDate.month

readonly year: number

Year, 1 to 9999.

CalendarDate.year


A time of day: hour 0–23 and minute 0–59.

const time: ClockTime = { hour: 14, minute: 30 };

readonly hour: number

Hour, 0 to 23.

readonly minute: number

Minute, 0 to 59.

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

Date style for formatDate; long is the default.


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

Error code shared by every malagasy-intl 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 an operator.
  • PRECISION_LOSS: The value is exact in the text but the port’s number type cannot hold it exactly; ask for the exact text instead.

NameWidth = "wide" | "abbreviated"

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

const width: NameWidth = 'abbreviated';

SafeParseResult<T> = { data: T; success: true; } | { error: MalagasyIntlError; success: false; }

Outcome of a safeParse… function: the value, or the error that would have been thrown.

T

const result = safeParsePhone(input);
if (result.success) console.log(result.data);
else console.log(result.error.code);

const SPEC_VERSION: "0.14.0" = '0.14.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.

MalagasyIntlError 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'

monthName(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.

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

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

parseDate(text, style?): CalendarDate

Reads a Malagasy date back into calendar fields: the inverse of formatDate.

string

A date in any style formatDate writes: 23 Novambra 2026, Alatsinainy 23 Novambra 2026, 2026 Nov 23, 2026-11-23 or 23/11/2026. Case and spacing do not matter; day and month may have one digit.

DateStyle

Optional: read only this style.

CalendarDate

The calendar date.

MalagasyIntlError INVALID_TYPE for non-strings or an unknown style, INVALID_FORMAT for text in no style, a day that does not exist or a wrong weekday, OUT_OF_RANGE for a year outside 1–9999. Without a style, the styles are tried in order and the first that reads wins (5 Mey 12 is 5 May of year 12).

parseDate('23 Novambra 2026'); // { year: 2026, month: 11, day: 23 }
parseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }

parseTime(text): ClockTime

Reads a time said in Malagasy back into hour and minute: the inverse of spellTime.

string

A time as spellTime says it, in any case and spacing, with or without amin'ny.

ClockTime

The time.

MalagasyIntlError INVALID_TYPE for non-strings, INVALID_FORMAT for anything else.

parseTime("amin'ny roa sy telopolo minitra tolakandro"); // { hour: 14, minute: 30 }

safeParseDate(text, style?): SafeParseResult<CalendarDate>

Reads a Malagasy date without throwing.

unknown

A date as formatDate writes it, in any style, any case and spacing.

DateStyle

Optional: read only this style.

SafeParseResult<CalendarDate>

{ success: true, data } with the calendar date, or { success: false, error }.

const result = safeParseDate('23 Novambra 2026');
if (result.success) result.data; // { year: 2026, month: 11, day: 23 }

safeParseTime(text): SafeParseResult<ClockTime>

Reads a time said in Malagasy without throwing.

unknown

A time as spellTime says it.

SafeParseResult<ClockTime>

{ success: true, data } with the time, or { success: false, error }.

const result = safeParseTime("amin'ny roa sy telopolo minitra tolakandro");
if (result.success) result.data; // { hour: 14, minute: 30 }

spellDateTime(dateTime, style?): string

Writes a date and a time in Malagasy: the date in a style, then the time in words.

string | Date | CalendarDateTime

YYYY-MM-DDTHH:MM text (a space may replace the T), { year, month, day, hour, minute }, or a Date (read in its local time).

DateStyle

The date style: long (default), full, medium, short or numeric.

string

The date, then the time in words.

MalagasyIntlError INVALID_FORMAT for text that is not a date and a time or a day that does not exist, OUT_OF_RANGE for fields out of range, INVALID_TYPE for other input or an unknown style.

spellDateTime('2026-11-23T14:30'); // "23 Novambra 2026 amin'ny roa sy telopolo minitra tolakandro"
spellDateTime('2026-11-23T14:30', 'full'); // "Alatsinainy 23 Novambra 2026 amin'ny roa sy telopolo minitra tolakandro"

spellTime(time): string

Says a time of day in Malagasy: amin'ny, the hour on a 12-hour clock, the minutes, then the part of the day.

string | Date | ClockTime

HH:MM text, { hour, minute }, or a Date (read in its local time).

string

The time in words.

MalagasyIntlError INVALID_FORMAT for text that is not HH:MM, OUT_OF_RANGE for an hour outside 0–23 or a minute outside 0–59, INVALID_TYPE for other input.

spellTime('14:30'); // "amin'ny roa sy telopolo minitra tolakandro"
spellTime('07:15'); // "amin'ny fito sy dimy ambin'ny folo minitra maraina"
spellTime('17:45'); // "amin'ny dimy sy dimy amby efapolo minitra tolakandro"

tryParseDate(text, style?): CalendarDate | undefined

Reads a Malagasy date, or gives undefined instead of throwing.

unknown

A date in any style formatDate writes.

DateStyle

Optional: read only this style.

CalendarDate | undefined

The calendar date, or undefined when parseDate would throw.

tryParseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }
tryParseDate('30/02/2026'); // undefined

tryParseTime(text): ClockTime | undefined

Reads a time said in Malagasy, or gives undefined instead of throwing.

unknown

A time as spellTime says it.

ClockTime | undefined

The time, or undefined when parseTime would throw.

tryParseTime("amin'ny fito maraina"); // { hour: 7, minute: 0 }
tryParseTime('seven'); // undefined

weekdayName(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.

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

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