Formatting Ethiopian Dates with Kenat
The kenat.formatting module provides a collection of functions to format Kenat date objects into various string representations. You can combine these with the Time class for time-aware formatting.
You can format dates in two ways:
- Using instance methods on a
Kenatobject (e.g.,date.format(...),date.formatInGeezAmharic(),date.toISOString()). - Using named formatting functions (e.g.,
formatStandard(date),formatWithWeekday(date), etc.) imported fromkenat.
All formatting functions return strings suitable for display. Invalid date or time objects will throw an error (e.g., InvalidEthiopianDateError, InvalidTimeError).
Formatting Functions
Here are the primary functions available for formatting dates in JavaScript. All formatting functions can be used as instance methods on a Kenat object or as named functions imported from kenat. Invalid date or time objects will throw an error (e.g., InvalidEthiopianDateError, InvalidTimeError).
Standard Date Format
Formats a date into a standard โMonth Day Yearโ string.
undefined
import { Kenat, formatStandard } from 'kenat';
// Kenat constructor supports string/object/Date
const today = new Kenat('2016/1/10');
// or: new Kenat({ year: 2016, month: 1, day: 10 })
// or: new Kenat(new Date(2016, 0, 10))
// Using Kenat instance method (default Amharic)
console.log(today.format());
// Output: แแตแจแจแ 10 2016
// Using Kenat instance method (English)
console.log(today.format({ lang: 'english' }));
// Output: Meskerem 10 2016
// Using named formatting function
console.log(formatStandard(today));
// Output: แแตแจแจแ 10 2016
console.log(formatStandard(today, 'english'));
// Output: Meskerem 10 2016Date Format with Weekday
Includes the day of the week in the output.
undefined
import { Kenat, formatWithWeekday } from 'kenat';
const today = new Kenat('2016/1/10');
// Using Kenat instance method
console.log(today.formatWithWeekday());
// Output: แแญแฐแ, แแตแจแจแ 10 2016
console.log(today.formatWithWeekday('english', true));
// Output: Tuesday, Meskerem แฒ แณแปแฒแฎ
// Using named formatting function
console.log(formatWithWeekday(today));
// Output: แแญแฐแ, แแตแจแจแ 10 2016
console.log(formatWithWeekday(today, 'english', true));
// Output: Tuesday, Meskerem แฒ แณแปแฒแฎ
// Options: lang ('amharic'|'english'), useGeez (boolean)Date Format with Time
Combines a date and a Time object into a single string.
undefined
import { Kenat, Time, formatWithTime } from 'kenat';
const today = new Kenat('2016/1/10');
const time = new Time(8, 30, 'day');
// Using Kenat instance method
console.log(today.formatWithTime(time));
// Output: แแตแจแจแ 10 2016 8:30 แแ
// Using named formatting function
console.log(formatWithTime(today, time));
// Output: แแตแจแจแ 10 2016 8:30 แแ
// Options: lang ('amharic'|'english'), useGeez (boolean)Geez Numeral Date Format
Formats the date using Geโez numerals for the day and year.
undefined
import { Kenat, formatInGeezAmharic } from 'kenat';
const today = new Kenat('2016/1/10');
// Using Kenat instance method
console.log(today.formatInGeezAmharic());
// Output: แแตแจแจแ แฒ แณแปแฒแฎ
// Using named formatting function
console.log(formatInGeezAmharic(today));
// Output: แแตแจแจแ แฒ แณแปแฒแฎShort Date Format
Returns the date in a compact YYYY/MM/DD format.
undefined
import { Kenat, formatShort } from 'kenat';
const today = new Kenat('2016/1/10');
// Using Kenat instance method
console.log(today.formatShort());
// Output: 2016/01/10
// Using named formatting function
console.log(formatShort(today));
// Output: 2016/01/10ISO Date String Format
Returns an ISO-like string, with optional time.
undefined
import { Kenat, Time, toISODateString } from 'kenat';
// Date only
const today = new Kenat('2016/1/10');
console.log(today.toISOString());
// Output: 2016-01-10
console.log(toISODateString(today));
// Output: 2016-01-10
// With time
const time = new Time(8, 30, 'day');
// Kenat does not accept time in constructor, set time via setTime
today.setTime(8, 30, 'day');
console.log(today.toISOString());
// Output: 2016-01-10T08:30
console.log(toISODateString(today, time));
// Output: 2016-01-10T08:30