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.
Classes
Section titled “Classes”MalagasyIntlError
Section titled “MalagasyIntlError”Error thrown by malagasy-intl functions. Check code, which is the same in every language.
Example
Section titled “Example”try { spell(-1);} catch (error) { if (error instanceof MalagasyIntlError && error.code === 'OUT_OF_RANGE') { // handle it }}Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MalagasyIntlError(
code,message):MalagasyIntlError
Creates an error with a shared spec code.
Parameters
Section titled “Parameters”Shared spec error code.
message
Section titled “message”string
Human-readable explanation, in English.
Returns
Section titled “Returns”Overrides
Section titled “Overrides”Error.constructor
Properties
Section titled “Properties”
readonlycode:ErrorCode
Why the call failed; the same code in every language.
readonlyname:"MalagasyIntlError"='MalagasyIntlError'
Always 'MalagasyIntlError'.
Overrides
Section titled “Overrides”Error.name
Methods
Section titled “Methods”[hasInstance]()
Section titled “[hasInstance]()”
static[hasInstance](value):boolean
Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.
Parameters
Section titled “Parameters”unknown
Any value, usually a caught error.
Returns
Section titled “Returns”boolean
true when value is an malagasy-intl error.
Remarks
Section titled “Remarks”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.
Interfaces
Section titled “Interfaces”CalendarDate
Section titled “CalendarDate”A calendar date: year 1–9999, month 1–12, day of the month.
Example
Section titled “Example”const independenceDay: CalendarDate = { year: 1960, month: 6, day: 26 };Extended by
Section titled “Extended by”Properties
Section titled “Properties”
readonlyday:number
Day of the month, 1 to 31.
readonlymonth:number
Month, 1 (Janoary) to 12 (Desambra).
readonlyyear:number
Year, 1 to 9999.
CalendarDateTime
Section titled “CalendarDateTime”A date and a time of day: calendar fields plus hour and minute.
Example
Section titled “Example”const meeting: CalendarDateTime = { year: 2026, month: 11, day: 23, hour: 14, minute: 30 };Extends
Section titled “Extends”Properties
Section titled “Properties”
readonlyday:number
Day of the month, 1 to 31.
Inherited from
Section titled “Inherited from”
readonlyhour:number
Hour, 0 to 23.
Inherited from
Section titled “Inherited from”minute
Section titled “minute”
readonlyminute:number
Minute, 0 to 59.
Inherited from
Section titled “Inherited from”
readonlymonth:number
Month, 1 (Janoary) to 12 (Desambra).
Inherited from
Section titled “Inherited from”
readonlyyear:number
Year, 1 to 9999.
Inherited from
Section titled “Inherited from”ClockTime
Section titled “ClockTime”A time of day: hour 0–23 and minute 0–59.
Example
Section titled “Example”const time: ClockTime = { hour: 14, minute: 30 };Extended by
Section titled “Extended by”Properties
Section titled “Properties”
readonlyhour:number
Hour, 0 to 23.
minute
Section titled “minute”
readonlyminute:number
Minute, 0 to 59.
Type Aliases
Section titled “Type Aliases”DateStyle
Section titled “DateStyle”DateStyle =
"long"|"full"|"medium"|"short"|"numeric"
Date style for formatDate; long is the default.
ErrorCode
Section titled “ErrorCode”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
Section titled “NameWidth”NameWidth =
"wide"|"abbreviated"
Name width: wide (Novambra, default) or abbreviated (Nov).
Example
Section titled “Example”const width: NameWidth = 'abbreviated';SafeParseResult
Section titled “SafeParseResult”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.
Type Parameters
Section titled “Type Parameters”T
Example
Section titled “Example”const result = safeParsePhone(input);if (result.success) console.log(result.data);else console.log(result.error.code);Variables
Section titled “Variables”SPEC_VERSION
Section titled “SPEC_VERSION”
constSPEC_VERSION:"0.14.0"='0.14.0'
Version of the shared spec this code was generated from.
Functions
Section titled “Functions”formatDate()
Section titled “formatDate()”formatDate(
date,style?):string
Writes a date in Malagasy.
Parameters
Section titled “Parameters”string | CalendarDate | Date
YYYY-MM-DD text, { year, month, day }, or a Date (read in its local
calendar fields).
style?
Section titled “style?”DateStyle = DEFAULT_STYLE
long (default), full, medium, short (CLDR) or numeric (dd/MM/y).
Returns
Section titled “Returns”string
The formatted date.
Throws
Section titled “Throws”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.
Example
Section titled “Example”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()
Section titled “monthName()”monthName(
month,width?):string
The Malagasy name of a month.
Parameters
Section titled “Parameters”number
Month number, 1 (Janoary) to 12 (Desambra).
width?
Section titled “width?”NameWidth = 'wide'
wide (default) or abbreviated.
Returns
Section titled “Returns”string
The month name.
Throws
Section titled “Throws”MalagasyIntlError OUT_OF_RANGE outside 1–12, INVALID_TYPE for non-integers or an
unknown width.
Example
Section titled “Example”monthName(6); // 'Jona'monthName(11, 'abbreviated'); // 'Nov'parseDate()
Section titled “parseDate()”parseDate(
text,style?):CalendarDate
Reads a Malagasy date back into calendar fields: the inverse of formatDate.
Parameters
Section titled “Parameters”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.
style?
Section titled “style?”Optional: read only this style.
Returns
Section titled “Returns”The calendar date.
Throws
Section titled “Throws”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).
Example
Section titled “Example”parseDate('23 Novambra 2026'); // { year: 2026, month: 11, day: 23 }parseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }parseTime()
Section titled “parseTime()”parseTime(
text):ClockTime
Reads a time said in Malagasy back into hour and minute: the inverse of spellTime.
Parameters
Section titled “Parameters”string
A time as spellTime says it, in any case and spacing, with or without
amin'ny.
Returns
Section titled “Returns”The time.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for non-strings, INVALID_FORMAT for anything else.
Example
Section titled “Example”parseTime("amin'ny roa sy telopolo minitra tolakandro"); // { hour: 14, minute: 30 }safeParseDate()
Section titled “safeParseDate()”safeParseDate(
text,style?):SafeParseResult<CalendarDate>
Reads a Malagasy date without throwing.
Parameters
Section titled “Parameters”unknown
A date as formatDate writes it, in any style, any case and spacing.
style?
Section titled “style?”Optional: read only this style.
Returns
Section titled “Returns”{ success: true, data } with the calendar date, or { success: false, error }.
Example
Section titled “Example”const result = safeParseDate('23 Novambra 2026');if (result.success) result.data; // { year: 2026, month: 11, day: 23 }safeParseTime()
Section titled “safeParseTime()”safeParseTime(
text):SafeParseResult<ClockTime>
Reads a time said in Malagasy without throwing.
Parameters
Section titled “Parameters”unknown
A time as spellTime says it.
Returns
Section titled “Returns”{ success: true, data } with the time, or { success: false, error }.
Example
Section titled “Example”const result = safeParseTime("amin'ny roa sy telopolo minitra tolakandro");if (result.success) result.data; // { hour: 14, minute: 30 }spellDateTime()
Section titled “spellDateTime()”spellDateTime(
dateTime,style?):string
Writes a date and a time in Malagasy: the date in a style, then the time in words.
Parameters
Section titled “Parameters”dateTime
Section titled “dateTime”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).
style?
Section titled “style?”The date style: long (default), full, medium, short or numeric.
Returns
Section titled “Returns”string
The date, then the time in words.
Throws
Section titled “Throws”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.
Example
Section titled “Example”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()
Section titled “spellTime()”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.
Parameters
Section titled “Parameters”string | Date | ClockTime
HH:MM text, { hour, minute }, or a Date (read in its local time).
Returns
Section titled “Returns”string
The time in words.
Throws
Section titled “Throws”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.
Example
Section titled “Example”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()
Section titled “tryParseDate()”tryParseDate(
text,style?):CalendarDate|undefined
Reads a Malagasy date, or gives undefined instead of throwing.
Parameters
Section titled “Parameters”unknown
A date in any style formatDate writes.
style?
Section titled “style?”Optional: read only this style.
Returns
Section titled “Returns”CalendarDate | undefined
The calendar date, or undefined when parseDate would throw.
Example
Section titled “Example”tryParseDate('23/11/2026'); // { year: 2026, month: 11, day: 23 }tryParseDate('30/02/2026'); // undefinedtryParseTime()
Section titled “tryParseTime()”tryParseTime(
text):ClockTime|undefined
Reads a time said in Malagasy, or gives undefined instead of throwing.
Parameters
Section titled “Parameters”unknown
A time as spellTime says it.
Returns
Section titled “Returns”ClockTime | undefined
The time, or undefined when parseTime would throw.
Example
Section titled “Example”tryParseTime("amin'ny fito maraina"); // { hour: 7, minute: 0 }tryParseTime('seven'); // undefinedweekdayName()
Section titled “weekdayName()”weekdayName(
isoWeekday,width?):string
The Malagasy name of a weekday, numbered as in ISO 8601 (Monday 1 … Sunday 7).
Parameters
Section titled “Parameters”isoWeekday
Section titled “isoWeekday”number
1 (Alatsinainy) to 7 (Alahady).
width?
Section titled “width?”NameWidth = 'wide'
wide (default) or abbreviated.
Returns
Section titled “Returns”string
The weekday name.
Throws
Section titled “Throws”MalagasyIntlError OUT_OF_RANGE outside 1–7, INVALID_TYPE for non-integers or an
unknown width.
Example
Section titled “Example”weekdayName(1); // 'Alatsinainy'weekdayName(7, 'abbreviated'); // 'Alah'