⚙️ Kenat Class
The Kenat
class provides a high-level API for working with Ethiopian calendar dates. You can create instances, format dates, manipulate them, and print calendars.
🏗 Constructor
new Kenat(dateStringOrDict?)
Creates a new instance of an Ethiopian date.
- If no argument is passed, it uses the current Gregorian system date.
- If a string like
'2015/6/10'
is passed, it’s parsed as an Ethiopian date. - It can also be initialized with a dictionary:
{'year': 2015, 'month': 6, 'day': 10}
.
undefined
// No argument: uses current system date (Gregorian)
const k = new Kenat();
// From a string (Ethiopian date, supports 'YYYY/MM/DD', 'YYYY-MM-DD', or ISO 'YYYY-MM-DD')
const custom = new Kenat('2016/6/10');
const custom2 = new Kenat('2016-6-10');
const iso = new Kenat('2016-06-10'); // ISO format also supported
// From an object with year, month, day (Ethiopian)
const fromObj = new Kenat({ year: 2016, month: 6, day: 10 });
// From a native JavaScript Date object (Gregorian)
// Note: JS Date months are 0-based (June = 5)
const fromDate = new Kenat(new Date(2016, 5, 10));
📆 Date Access & Formatting
Multiple methods are available to get the date in different formats.
Method | Returns | Description |
---|---|---|
get_ethiopian() | { 'year', 'month', 'day' } | Get the Ethiopian date as a dictionary. |
get_gregorian() | { 'year', 'month', 'day' } | Get the equivalent Gregorian date. |
__str__() | "YYYY/MM/DD" | A simple string representation. |
format_standard(lang='amharic') | "መስከረም 5 2016" or similar | Human-readable string with language options. |
format_in_geez_amharic() | "መስከረም ፭ ፳፻፲፮" | Geez-styled output. |
format_with_weekday(lang, use_geez) | "ማክሰኞ, መስከረም 1 2016" | Includes the day of the week. |
format_with_time(time_obj, lang) | "መስከረም 10 2016 08:30 ጠዋት" | Combines date and a Time object. |
to_iso_date_string(time_obj) | "YYYY-MM-DDTHH:mm" | ISO 8601-like format. |
undefined
const k = new Kenat('2016/1/5');
console.log(k.getEthiopian()); // { year: 2016, month: 1, day: 5 }
console.log(k.toString()); // Ethiopian: 2016-1-5
console.log(k.formatInGeezAmharic()); // መስከረም ፭ ፳፻፲፮
🕒 Time Handling
The Time
class handles time calculations, conversions, and formatting separately from the date.
Method/Class | Description |
---|---|
Time(hour, min, period) | Creates a time object (e.g., 3:30, ‘night’). |
Time.from_gregorian(h, m) | Creates an Ethiopian Time object from Gregorian 24h time. |
time.to_gregorian() | Converts Ethiopian time back to a 24h dictionary. |
time.format() | Formats the time into a string (e.g., “፫:፴ ሌሊት”). |
time.add(duration) | Adds hours/minutes to the time. |
time.diff(other_time) | Calculates the difference between two Time objects. |
undefined
const k = new Kenat();
console.log(k.getCurrentTime());
➕ Date Arithmetic
These functions are available in the kenat.day_arithmetic
module and take a Kenat
object as the first argument.
Function | Description |
---|---|
add_days(date, n) | Returns a new date n days away. |
add_months(date, n) | Returns a new date n months away. |
add_years(date, n) | Returns a new date n years away. |
undefined
const k = new Kenat('2014/5/5');
k.addDays(10);
k.addMonths(-1);
console.log(k.getEthiopian());
📏 Compare Dates
These functions take two Kenat
objects and return the difference. They are available in the kenat.day_arithmetic
module.
Function | Description |
---|---|
diff_in_days(a, b) | Calculates the total day difference. |
diff_in_months(a, b) | Calculates the month difference. |
diff_in_years(a, b) | Calculates the year difference. |
undefined
const a = new Kenat('2015/6/10');
const b = new Kenat('2012/6/10');
console.log(a.diffInYears(b)); // → 3
🗓 Calendar & Holiday Tools
The library includes powerful tools for generating monthly calendars and retrieving holiday information.
Method/Class | Description |
---|---|
MonthGrid(config) | A class to generate a grid for a specific month. |
get_holiday(key, year, lang) | Get details for a single holiday. |
get_holidays_in_month(y, m) | Get all holidays in a given month. |
get_holidays_for_year(year) | Get all holidays for a given year. |
undefined
const k = new Kenat('2016/1/1');
k.printThisMonth(true);
🧩 Advanced Features & Utilities
Kenat Static Methods
Kenat.now()
— Returns a Kenat instance for the current date/time.Kenat.getMonthCalendar(year, month, options)
— Returns a calendar grid for a given month.Kenat.getYearCalendar(year, options)
— Returns a full year calendar.Kenat.generateDateRange(startDate, endDate)
— Returns an array of Kenat dates for a range.
MonthGrid Usage
You can generate a month grid with holidays and weekday headers:
import { MonthGrid } from 'kenat';
const grid = new MonthGrid({ year: 2016, month: 1 });
console.log(grid.headers); // ['እሑድ', ...]
console.log(grid.days); // Array of day objects
Holiday Filtering
All holiday functions support filtering by tags:
import { getHolidaysForYear, HolidayTags } from 'kenat';
const publicHolidays = getHolidaysForYear(2016, { filter: [HolidayTags.PUBLIC] });
Geez Numeral Conversion
toGeez(number)
— Converts numbers to Geez numerals.toArabic(geezStr)
— Converts Geez numerals to Arabic numbers.
Date Validation & Utilities
isValidEthiopianDate(year, month, day)
— Checks if a date is valid.isEthiopianLeapYear(year)
— Checks for leap years.getWeekday({ year, month, day })
— Returns the weekday index.
Error Classes
Kenat exports error classes for robust error handling:
InvalidEthiopianDateError
InvalidGregorianDateError
InvalidInputTypeError
InvalidTimeError
InvalidGridConfigError
UnknownHolidayError